diff options
Diffstat (limited to 'mysys')
119 files changed, 8459 insertions, 3872 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am index 9141ab132bd..a927fa5e3a0 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -27,7 +27,7 @@ noinst_HEADERS = mysys_priv.h my_static.h \ my_os2thread.c my_os2tls.c libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\ mf_path.c mf_loadpath.c\ - my_open.c my_create.c my_seek.c my_read.c \ + my_open.c my_create.c my_dup.c my_seek.c my_read.c \ my_pread.c my_write.c \ mf_keycache.c \ mf_iocache.c mf_iocache2.c mf_cache.c mf_tempfile.c \ @@ -38,7 +38,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\ my_error.c errors.c my_div.c my_messnc.c \ mf_format.c mf_same.c mf_dirname.c mf_fn_ext.c \ my_symlink.c my_symlink2.c \ - mf_pack.c mf_pack2.c mf_unixpath.c mf_strip.c \ + mf_pack.c mf_unixpath.c mf_strip.c \ mf_casecnv.c mf_soundex.c mf_wcomp.c mf_wfile.c \ mf_qsort.c mf_qsort2.c mf_sort.c \ ptr_cmp.c mf_radix.c queues.c \ @@ -47,23 +47,24 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\ my_delete.c my_rename.c my_redel.c my_tempnam.c \ my_chsize.c my_lread.c my_lwrite.c my_clock.c \ my_quick.c my_lockmem.c my_static.c \ - getopt.c getopt1.c getvar.c my_mkdir.c \ - default.c my_compress.c checksum.c raid.cc raid2.c \ - my_net.c my_port.c \ - my_vsnprintf.c charset.c my_bitmap.c my_gethostbyname.c + my_getopt.c my_mkdir.c \ + default.c my_compress.c checksum.c raid.cc \ + my_net.c my_semaphore.c my_port.c my_sleep.c \ + my_vsnprintf.c charset.c my_bitmap.c my_bit.c md5.c \ + my_gethostbyname.c rijndael.c my_aes.c sha1.c \ + my_netware.c EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \ thr_mutex.c thr_rwlock.c libmysys_a_LIBADD = @THREAD_LOBJECTS@ -# test_fn removed 980815 since it not upp to date test_dir -noinst_PROGRAMS = test_charset @THREAD_LPROGRAMS@ +# test_fn removed 980815 since it not up to date test_dir +noinst_PROGRAMS = @THREAD_LPROGRAMS@ # test_dir_DEPENDENCIES= $(LIBRARIES) # testhash_DEPENDENCIES= $(LIBRARIES) -test_charset_DEPENDENCIES= $(LIBRARIES) +# test_charset_DEPENDENCIES= $(LIBRARIES) EXTRA_PROGRAMS = DEFS = -DDEFAULT_BASEDIR=\"$(prefix)\" \ -DDATADIR="\"$(MYSQLDATAdir)\"" \ -DDEFAULT_CHARSET_HOME="\"$(MYSQLBASEdir)\"" \ - -DDATADIR="\"$(MYSQLDATAdir)\"" \ -DSHAREDIR="\"$(MYSQLSHAREdir)\"" \ @DEFS@ @@ -77,28 +78,32 @@ FLAGS=$(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) @NOINST_LDFLAGS@ # which automaticly removes the object files you use to compile a final program # -test_thr_alarm: thr_alarm.c $(LIBRARIES) +test_thr_alarm$(EXEEXT): thr_alarm.c $(LIBRARIES) $(CP) $(srcdir)/thr_alarm.c ./test_thr_alarm.c $(LINK) $(FLAGS) -DMAIN ./test_thr_alarm.c $(LDADD) $(LIBS) - $(RM) -f ./test_thr_alarm.* + $(RM) -f ./test_thr_alarm.c -test_thr_lock: thr_lock.c $(LIBRARIES) +test_thr_lock$(EXEEXT): thr_lock.c $(LIBRARIES) $(CP) $(srcdir)/thr_lock.c test_thr_lock.c $(LINK) $(FLAGS) -DMAIN ./test_thr_lock.c $(LDADD) $(LIBS) - $(RM) -f ./test_thr_lock.* + $(RM) -f ./test_thr_lock.c -test_vsnprintf: my_vsnprintf.c $(LIBRARIES) +test_vsnprintf$(EXEEXT): my_vsnprintf.c $(LIBRARIES) $(CP) $(srcdir)/my_vsnprintf.c test_vsnprintf.c $(LINK) $(FLAGS) -DMAIN ./test_vsnprintf.c $(LDADD) $(LIBS) - $(RM) -f test_vsnprintf.* + $(RM) -f test_vsnprintf.c +test_io_cache$(EXEEXT): mf_iocache.c $(LIBRARIES) + $(CP) $(srcdir)/mf_iocache.c test_io_cache.c + $(LINK) $(FLAGS) -DMAIN ./test_io_cache.c $(LDADD) $(LIBS) + $(RM) -f test_io_cache.c -test_dir: test_dir.c $(LIBRARIES) +test_dir$(EXEEXT): test_dir.c $(LIBRARIES) $(LINK) $(FLAGS) -DMAIN $(srcdir)/test_dir.c $(LDADD) $(LIBS) test_charset$(EXEEXT): test_charset.c $(LIBRARIES) $(LINK) $(FLAGS) -DMAIN $(srcdir)/test_charset.c $(LDADD) $(LIBS) -testhash: testhash.c $(LIBRARIES) +testhash$(EXEEXT): testhash.c $(LIBRARIES) $(LINK) $(FLAGS) -DMAIN $(srcdir)/testhash.c $(LDADD) $(LIBS) # Don't update the files from bitkeeper diff --git a/mysys/array.c b/mysys/array.c index cc6710124d7..6d00585f24d 100644 --- a/mysys/array.c +++ b/mysys/array.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Handling of arrays that can grow dynamicly. */ @@ -25,12 +24,28 @@ #include "m_string.h" /* - Initiate array and alloc space for init_alloc elements. Array is usable - even if space allocation failed + Initiate dynamic array + + SYNOPSIS + init_dynamic_array() + array Pointer to an array + element_size Size of element + init_alloc Number of initial elements + alloc_increment Increment for adding new elements + + DESCRIPTION + init_dynamic_array() initiates array and allocate space for + init_alloc eilements. + Array is usable even if space allocation failed. + + RETURN VALUE + TRUE my_malloc_ci() failed + FALSE Ok */ my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size, - uint init_alloc, uint alloc_increment CALLER_INFO_PROTO) + uint init_alloc, + uint alloc_increment CALLER_INFO_PROTO) { DBUG_ENTER("init_dynamic_array"); if (!alloc_increment) @@ -52,8 +67,20 @@ my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size, DBUG_RETURN(TRUE); } DBUG_RETURN(FALSE); -} +} +/* + Insert element at the end of array. Allocate memory if needed. + + SYNOPSIS + insert_dynamic() + array + element + + RETURN VALUE + TRUE Insert failed + FALSE Ok +*/ my_bool insert_dynamic(DYNAMIC_ARRAY *array, gptr element) { @@ -73,7 +100,22 @@ my_bool insert_dynamic(DYNAMIC_ARRAY *array, gptr element) } - /* Alloc room for one element */ +/* + Alloc space for next element(s) + + SYNOPSIS + alloc_dynamic() + array + + DESCRIPTION + alloc_dynamic() checks if there is empty space for at least + one element if not tries to allocate space for alloc_increment + elements at the end of array. + + RETURN VALUE + pointer Pointer to empty space for element + 0 Error +*/ byte *alloc_dynamic(DYNAMIC_ARRAY *array) { @@ -92,7 +134,17 @@ byte *alloc_dynamic(DYNAMIC_ARRAY *array) } - /* remove last element from array and return it */ +/* + Pop last element from array. + + SYNOPSIS + pop_dynamic() + array + + RETURN VALUE + pointer Ok + 0 Array is empty +*/ byte *pop_dynamic(DYNAMIC_ARRAY *array) { @@ -101,6 +153,23 @@ byte *pop_dynamic(DYNAMIC_ARRAY *array) return 0; } +/* + Replace elemnent in array with given element and index + + SYNOPSIS + set_dynamic() + array + element Element to be inserted + idx Index where element is to be inserted + + DESCRIPTION + set_dynamic() replaces element in array. + If idx > max_element insert new element. Allocate memory if needed. + + RETURN VALUE + TRUE Idx was out of range and allocation of new memory failed + FALSE Ok +*/ my_bool set_dynamic(DYNAMIC_ARRAY *array, gptr element, uint idx) { @@ -128,6 +197,15 @@ my_bool set_dynamic(DYNAMIC_ARRAY *array, gptr element, uint idx) return FALSE; } +/* + Get an element from array by given index + + SYNOPSIS + get_dynamic() + array + gptr Element to be returned. If idx > elements contain zeroes. + idx Index of element wanted. +*/ void get_dynamic(DYNAMIC_ARRAY *array, gptr element, uint idx) { @@ -143,6 +221,14 @@ void get_dynamic(DYNAMIC_ARRAY *array, gptr element, uint idx) } +/* + Empty array by freeing all memory + + SYNOPSIS + delete_dynamic() + array Array to be deleted +*/ + void delete_dynamic(DYNAMIC_ARRAY *array) { if (array->buffer) @@ -153,6 +239,14 @@ void delete_dynamic(DYNAMIC_ARRAY *array) } } +/* + Delete element by given index + + SYNOPSIS + delete_dynamic_element() + array + idx Index of element to be deleted +*/ void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx) { @@ -163,6 +257,15 @@ void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx) } +/* + Free unused memory + + SYNOPSIS + freeze_size() + array Array to be freed + +*/ + void freeze_size(DYNAMIC_ARRAY *array) { uint elements=max(array->elements,1); diff --git a/mysys/charset.c b/mysys/charset.c index 9d9ede2b0c4..235fcb08023 100644 --- a/mysys/charset.c +++ b/mysys/charset.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" @@ -44,13 +43,6 @@ struct simpleconfig_buf_st { char *p; }; -/* Defined in strings/ctype.c */ - -CHARSET_INFO *find_compiled_charset(uint cs_number); -uint compiled_charset_number(const char *name); -const char *compiled_charset_name(uint charset_number); - - static uint num_from_csname(CS_ID **cs, const char *name) { CS_ID **c; @@ -110,7 +102,7 @@ char *get_charsets_dir(char *buf) strxmov(buf, DEFAULT_CHARSET_HOME, "/", sharedir, "/", CHARSET_DIR, NullS); } - convert_dirname(buf); + convert_dirname(buf,buf,NullS); DBUG_PRINT("info",("charsets dir='%s'", buf)); DBUG_RETURN(strend(buf)); } @@ -172,7 +164,11 @@ static my_bool read_charset_index(CS_ID ***charsets, myf myflags) } +#ifdef __NETWARE__ +my_bool STDCALL init_available_charsets(myf myflags) +#else static my_bool init_available_charsets(myf myflags) +#endif { my_bool error=0; /* @@ -203,6 +199,7 @@ static my_bool init_available_charsets(myf myflags) void free_charsets(void) { delete_dynamic(&cs_info_table); + charset_initialized=0; } @@ -264,22 +261,22 @@ static my_bool read_charset_file(uint cs_number, CHARSET_INFO *set, uint get_charset_number(const char *charset_name) { - my_bool error; - error = init_available_charsets(MYF(0)); /* If it isn't initialized */ - if (error) - return compiled_charset_number(charset_name); - else - return num_from_csname(available_charsets, charset_name); + uint number=compiled_charset_number(charset_name); + if (number) + return number; + if (init_available_charsets(MYF(0))) /* If it isn't initialized */ + return 0; + return num_from_csname(available_charsets, charset_name); } const char *get_charset_name(uint charset_number) { - my_bool error; - error = init_available_charsets(MYF(0)); /* If it isn't initialized */ - if (error) - return compiled_charset_name(charset_number); - else - return name_from_csnum(available_charsets, charset_number); + const char *name=compiled_charset_name(charset_number); + if (*name != '?') + return name; + if (init_available_charsets(MYF(0))) /* If it isn't initialized */ + return "?"; + return name_from_csnum(available_charsets, charset_number); } @@ -293,8 +290,8 @@ static CHARSET_INFO *find_charset(CHARSET_INFO **table, uint cs_number, return NULL; } -static CHARSET_INFO *find_charset_by_name(CHARSET_INFO **table, const char *name, - size_t tablesz) +static CHARSET_INFO *find_charset_by_name(CHARSET_INFO **table, + const char *name, size_t tablesz) { uint i; for (i = 0; i < tablesz; ++i) @@ -303,7 +300,7 @@ static CHARSET_INFO *find_charset_by_name(CHARSET_INFO **table, const char *name return NULL; } -static CHARSET_INFO *add_charset(uint cs_number, const char *cs_name) +static CHARSET_INFO *add_charset(uint cs_number, const char *cs_name, myf flags) { CHARSET_INFO tmp_cs,*cs; uchar tmp_ctype[CTYPE_TABLE_SIZE]; @@ -318,11 +315,11 @@ static CHARSET_INFO *add_charset(uint cs_number, const char *cs_name) cs->to_lower=tmp_to_lower; cs->to_upper=tmp_to_upper; cs->sort_order=tmp_sort_order; - if (read_charset_file(cs_number, cs, MYF(MY_WME))) + if (read_charset_file(cs_number, cs, flags)) return NULL; cs = (CHARSET_INFO*) my_once_alloc(sizeof(CHARSET_INFO), - MYF(MY_WME)); + MYF(MY_WME)); *cs=tmp_cs; cs->name = (char *) my_once_alloc((uint) strlen(cs_name)+1, MYF(MY_WME)); cs->ctype = (uchar*) my_once_alloc(CTYPE_TABLE_SIZE, MYF(MY_WME)); @@ -340,7 +337,7 @@ static CHARSET_INFO *add_charset(uint cs_number, const char *cs_name) return cs; } -static CHARSET_INFO *get_internal_charset(uint cs_number) +static CHARSET_INFO *get_internal_charset(uint cs_number, myf flags) { CHARSET_INFO *cs; /* @@ -351,13 +348,13 @@ static CHARSET_INFO *get_internal_charset(uint cs_number) if (!(cs = find_charset((CHARSET_INFO**) cs_info_table.buffer, cs_number, cs_info_table.elements))) if (!(cs = find_compiled_charset(cs_number))) - cs=add_charset(cs_number, get_charset_name(cs_number)); + cs=add_charset(cs_number, get_charset_name(cs_number), flags); pthread_mutex_unlock(&THR_LOCK_charset); return cs; } -static CHARSET_INFO *get_internal_charset_by_name(const char *name) +static CHARSET_INFO *get_internal_charset_by_name(const char *name, myf flags) { CHARSET_INFO *cs; /* @@ -368,7 +365,7 @@ static CHARSET_INFO *get_internal_charset_by_name(const char *name) if (!(cs = find_charset_by_name((CHARSET_INFO**) cs_info_table.buffer, name, cs_info_table.elements))) if (!(cs = find_compiled_charset_by_name(name))) - cs=add_charset(get_charset_number(name), name); + cs=add_charset(get_charset_number(name), name, flags); pthread_mutex_unlock(&THR_LOCK_charset); return cs; } @@ -378,7 +375,7 @@ CHARSET_INFO *get_charset(uint cs_number, myf flags) { CHARSET_INFO *cs; (void) init_available_charsets(MYF(0)); /* If it isn't initialized */ - cs=get_internal_charset(cs_number); + cs=get_internal_charset(cs_number, flags); if (!cs && (flags & MY_WME)) { @@ -410,7 +407,7 @@ CHARSET_INFO *get_charset_by_name(const char *cs_name, myf flags) { CHARSET_INFO *cs; (void) init_available_charsets(MYF(0)); /* If it isn't initialized */ - cs=get_internal_charset_by_name(cs_name); + cs=get_internal_charset_by_name(cs_name, flags); if (!cs && (flags & MY_WME)) { diff --git a/mysys/checksum.c b/mysys/checksum.c index 00861853945..1dd135c7ad9 100644 --- a/mysys/checksum.c +++ b/mysys/checksum.c @@ -1,30 +1,37 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Calculate a long checksum for a memoryblock. Used to verify pack_isam */ -#include <global.h> +#include <my_global.h> #include "my_sys.h" +/* + Calculate a long checksum for a memoryblock. Used to verify pack_isam + + SYNOPSIS + checksum() + mem Pointer to memory block + count Count of bytes +*/ + ulong checksum(const byte *mem, uint count) { ulong crc; - for (crc=0; count-- ; mem++) - crc=((crc << 1) + *((uchar*) mem)) + + for (crc= 0; count-- ; mem++) + crc= ((crc << 1) + *((uchar*) mem)) + test(crc & ((ulong) 1L << (8*sizeof(ulong)-1))); return crc; } diff --git a/mysys/default.c b/mysys/default.c index 0f8712d3298..cfdda17fb5f 100644 --- a/mysys/default.c +++ b/mysys/default.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /**************************************************************************** ** Add all options from files named "group".cnf from the default_directories @@ -48,6 +47,8 @@ char *defaults_extra_file=0; const char *default_directories[]= { #ifdef __WIN__ "C:/", +#elif defined(__NETWARE__) +"sys:/etc/", #else "/etc/", #endif @@ -55,7 +56,7 @@ const char *default_directories[]= { DATADIR, #endif "", /* Place for defaults_extra_dir */ -#ifndef __WIN__ +#if !defined(__WIN__) && !defined(__NETWARE__) "~/", #endif NullS, @@ -84,7 +85,7 @@ void load_defaults(const char *conf_file, const char **groups, char *ptr,**res; DBUG_ENTER("load_defaults"); - init_alloc_root(&alloc,128,0); + init_alloc_root(&alloc,512,0); if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults")) { /* remove the --no-defaults argument and return only the other arguments */ @@ -96,6 +97,7 @@ void load_defaults(const char *conf_file, const char **groups, res[0]= **argv; /* Copy program name */ for (i=2 ; i < (uint) *argc ; i++) res[i-1]=argv[0][i]; + res[i-1]=0; /* End pointer */ (*argc)--; *argv=res; *(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */ @@ -232,11 +234,10 @@ static my_bool search_default_file(DYNAMIC_ARRAY *args, MEM_ROOT *alloc, return 0; /* Ignore wrong paths */ if (dir) { - strmov(name,dir); - convert_dirname(name); + end=convert_dirname(name, dir, NullS); if (dir[0] == FN_HOMELIB) /* Add . to filenames in home */ - strcat(name,"."); - strxmov(strend(name),config_file,ext,NullS); + *end++='.'; + strxmov(end,config_file,ext,NullS); } else { @@ -384,16 +385,18 @@ void print_defaults(const char *conf_file, const char **groups) #endif for (dirs=default_directories ; *dirs; dirs++) { + const char *pos; + char *end; if (**dirs) - strmov(name,*dirs); + pos= *dirs; else if (defaults_extra_file) - strmov(name,defaults_extra_file); + pos= defaults_extra_file; else continue; - convert_dirname(name); + end=convert_dirname(name, pos, NullS); if (name[0] == FN_HOMELIB) /* Add . to filenames in home */ - strcat(name,"."); - strxmov(strend(name),conf_file,default_ext," ",NullS); + *end++='.'; + strxmov(end,conf_file,default_ext," ",NullS); fputs(name,stdout); } puts(""); @@ -410,4 +413,3 @@ void print_defaults(const char *conf_file, const char **groups) --defaults-file=# Only read default options from the given file #\n\ --defaults-extra-file=# Read this file after the global files are read"); } - diff --git a/mysys/errors.c b/mysys/errors.c index 77e52c2f0b3..8a120e0e869 100644 --- a/mysys/errors.c +++ b/mysys/errors.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/getopt.c b/mysys/getopt.c deleted file mode 100644 index 774cf3b82f4..00000000000 --- a/mysys/getopt.c +++ /dev/null @@ -1,750 +0,0 @@ -/* Getopt for GNU. - NOTE: getopt is now part of the C library, so if you don't know what - "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu - before changing it! - - Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94 - Free Software Foundation, Inc. - -Changes by monty: -- Added include of string.h when nessessary. -- Removed two warnings from gcc. - -This file is part of the GNU C Library. Its master source is NOT part of -the C library, however. The master source lives in /gd/gnu/lib. - -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. - -The GNU C 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ - -/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. - Ditto for AIX 3.2 and <stdlib.h>. */ -#ifndef _NO_PROTO -#define _NO_PROTO -#endif - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#if (!defined (__STDC__) || !__STDC__) && !defined(MSDOS) && !defined(OS2) -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - -#include <global.h> /* Changes for mysys */ -#include <m_string.h> - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#if defined (_LIBC) || !defined (__GNU_LIBRARY__) - - -/* This needs to come after some library #include - to get __GNU_LIBRARY__ defined. */ -#ifdef __GNU_LIBRARY__ -/* Don't include stdlib.h for non-GNU C libraries because some of them - contain conflicting prototypes for getopt. */ -#include <stdlib.h> -#endif /* GNU C library. */ - -/* This version of `getopt' appears to the caller like standard Unix `getopt' - but it behaves differently for the user, since it allows the user - to intersperse the options with the other arguments. - - As `getopt' works, it permutes the elements of ARGV so that, - when it is done, all the options precede everything else. Thus - all application programs are extended to handle flexible argument order. - - Setting the environment variable POSIXLY_CORRECT disables permutation. - Then the behavior is completely standard. - - GNU application programs can use a third alternative mode in which - they can distinguish the relative order of options and other arguments. */ - -#include "getopt.h" - -/* For communication from `getopt' to the caller. - When `getopt' finds an option that takes an argument, - the argument value is returned here. - Also, when `ordering' is RETURN_IN_ORDER, - each non-option ARGV-element is returned here. */ - -char *optarg = NULL; - -/* Index in ARGV of the next element to be scanned. - This is used for communication to and from the caller - and for communication between successive calls to `getopt'. - - On entry to `getopt', zero means this is the first call; initialize. - - When `getopt' returns EOF, this is the index of the first of the - non-option elements that the caller should itself scan. - - Otherwise, `optind' communicates from one call to the next - how much of ARGV has been scanned so far. */ - -/* XXX 1003.2 says this must be 1 before any call. */ -int optind = 1; - -/* The next char to be scanned in the option-element - in which the last option character we returned was found. - This allows us to pick up the scan where we left off. - - If this is zero, or a null string, it means resume the scan - by advancing to the next ARGV-element. */ - -static char *nextchar; - -/* Callers store zero here to inhibit the error message - for unrecognized options. */ - -int opterr = 1; - -/* Set to an option character which was unrecognized. - This must be initialized on some systems to avoid linking in the - system's own getopt implementation. */ - -int optopt = '?'; - -/* Describe how to deal with options that follow non-option ARGV-elements. - - If the caller did not specify anything, - the default is REQUIRE_ORDER if the environment variable - POSIXLY_CORRECT is defined, PERMUTE otherwise. - - REQUIRE_ORDER means don't recognize them as options; - stop option processing when the first non-option is seen. - This is what Unix does. - This mode of operation is selected by either setting the environment - variable POSIXLY_CORRECT, or using `+' as the first character - of the list of option characters. - - PERMUTE is the default. We permute the contents of ARGV as we scan, - so that eventually all the non-options are at the end. This allows options - to be given in any order, even with programs that were not written to - expect this. - - RETURN_IN_ORDER is an option available to programs that were written - to expect options and other ARGV-elements in any order and that care about - the ordering of the two. We describe each non-option ARGV-element - as if it were the argument of an option with character code 1. - Using `-' as the first character of the list of option characters - selects this mode of operation. - - The special argument `--' forces an end of option-scanning regardless - of the value of `ordering'. In the case of RETURN_IN_ORDER, only - `--' can cause `getopt' to return EOF with `optind' != ARGC. */ - -static enum -{ - REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER -} ordering; - -/* Value of POSIXLY_CORRECT environment variable. */ -static char *posixly_correct; - -#ifdef __GNU_LIBRARY__ -/* We want to avoid inclusion of string.h with non-GNU libraries - because there are many ways it can cause trouble. - On some systems, it contains special magic macros that don't work - in GCC. */ -#include <string.h> -#define my_index strchr -#else - -/* Avoid depending on library functions or files - whose names are inconsistent. */ - -#ifndef OS2 -char *getenv (const char *); -#endif - -static char * -my_index (const char *str, int chr) -{ - while (*str) - { - if (*str == chr) - return (char *) str; - str++; - } - return 0; -} - -/* If using GCC, we can safely declare strlen this way. - If not using GCC, it is ok not to declare it. */ -#ifdef __GNUC__ -/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. - That was relevant to code that was here before. */ -#if !defined (__STDC__) || !__STDC__ -/* gcc with -traditional declares the built-in strlen to return int, - and has done so at least since version 2.4.5. -- rms. */ -extern int strlen (const char *); -#endif /* not __STDC__ */ -#endif /* __GNUC__ */ - -#endif /* not __GNU_LIBRARY__ */ - -/* Handle permutation of arguments. */ - -/* Describe the part of ARGV that contains non-options that have - been skipped. `first_nonopt' is the index in ARGV of the first of them; - `last_nonopt' is the index after the last of them. */ - -static int first_nonopt; -static int last_nonopt; - -/* Exchange two adjacent subsequences of ARGV. - One subsequence is elements [first_nonopt,last_nonopt) - which contains all the non-options that have been skipped so far. - The other is elements [last_nonopt,optind), which contains all - the options processed since those non-options were skipped. - - `first_nonopt' and `last_nonopt' are relocated so that they describe - the new indices of the non-options in ARGV after they are moved. */ - -static void -exchange (char **argv) -{ - int bottom = first_nonopt; - int middle = last_nonopt; - int top = optind; - char *tem; - - /* Exchange the shorter segment with the far end of the longer segment. - That puts the shorter segment into the right place. - It leaves the longer segment in the right place overall, - but it consists of two parts that need to be swapped next. */ - - while (top > middle && middle > bottom) - { - if (top - middle > middle - bottom) - { - /* Bottom segment is the short one. */ - int len = middle - bottom; - register int i; - - /* Swap it with the top part of the top segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[top - (middle - bottom) + i]; - argv[top - (middle - bottom) + i] = tem; - } - /* Exclude the moved bottom segment from further swapping. */ - top -= len; - } - else - { - /* Top segment is the short one. */ - int len = top - middle; - register int i; - - /* Swap it with the bottom part of the bottom segment. */ - for (i = 0; i < len; i++) - { - tem = argv[bottom + i]; - argv[bottom + i] = argv[middle + i]; - argv[middle + i] = tem; - } - /* Exclude the moved top segment from further swapping. */ - bottom += len; - } - } - - /* Update records for the slots the non-options now occupy. */ - - first_nonopt += (optind - last_nonopt); - last_nonopt = optind; -} - -/* Initialize the internal data when the first call is made. */ - -static const char * -_getopt_initialize (const char *optstring) -{ - /* Start processing options with ARGV-element 1 (since ARGV-element 0 - is the program name); the sequence of previously skipped - non-option ARGV-elements is empty. */ - - first_nonopt = last_nonopt = optind = 1; - - nextchar = NULL; - - posixly_correct = getenv ("POSIXLY_CORRECT"); - - /* Determine how to handle the ordering of options and nonoptions. */ - - if (optstring[0] == '-') - { - ordering = RETURN_IN_ORDER; - ++optstring; - } - else if (optstring[0] == '+') - { - ordering = REQUIRE_ORDER; - ++optstring; - } - else if (posixly_correct != NULL) - ordering = REQUIRE_ORDER; - else - ordering = PERMUTE; - - return optstring; -} - -/* Scan elements of ARGV (whose length is ARGC) for option characters - given in OPTSTRING. - - If an element of ARGV starts with '-', and is not exactly "-" or "--", - then it is an option element. The characters of this element - (aside from the initial '-') are option characters. If `getopt' - is called repeatedly, it returns successively each of the option characters - from each of the option elements. - - If `getopt' finds another option character, it returns that character, - updating `optind' and `nextchar' so that the next call to `getopt' can - resume the scan with the following option character or ARGV-element. - - If there are no more option characters, `getopt' returns `EOF'. - Then `optind' is the index in ARGV of the first ARGV-element - that is not an option. (The ARGV-elements have been permuted - so that those that are not options now come last.) - - OPTSTRING is a string containing the legitimate option characters. - If an option character is seen that is not listed in OPTSTRING, - return '?' after printing an error message. If you set `opterr' to - zero, the error message is suppressed but we still return '?'. - - If a char in OPTSTRING is followed by a colon, that means it wants an arg, - so the following text in the same ARGV-element, or the text of the following - ARGV-element, is returned in `optarg'. Two colons mean an option that - wants an optional arg; if there is text in the current ARGV-element, - it is returned in `optarg', otherwise `optarg' is set to zero. - - If OPTSTRING starts with `-' or `+', it requests different methods of - handling the non-option ARGV-elements. - See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. - - Long-named options begin with `--' instead of `-'. - Their names may be abbreviated as long as the abbreviation is unique - or is an exact match for some defined option. If they have an - argument, it follows the option name in the same ARGV-element, separated - from the option name by a `=', or else the in next ARGV-element. - When `getopt' finds a long-named option, it returns 0 if that option's - `flag' field is nonzero, the value of the option's `val' field - if the `flag' field is zero. - - The elements of ARGV aren't really const, because we permute them. - But we pretend they're const in the prototype to be compatible - with other systems. - - LONGOPTS is a vector of `struct option' terminated by an - element containing a name which is zero. - - LONGIND returns the index in LONGOPT of the long-named option found. - It is only valid when a long-named option has been found by the most - recent call. - - If LONG_ONLY is nonzero, '-' as well as '--' can introduce - long-named options. */ - -int -_getopt_internal (int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only) -{ - optarg = NULL; - - if (optind == 0) - optstring = _getopt_initialize (optstring); - - if (nextchar == NULL || *nextchar == '\0') - { - /* Advance to the next ARGV-element. */ - - if (ordering == PERMUTE) - { - /* If we have just processed some options following some non-options, - exchange them so that the options come first. */ - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (last_nonopt != optind) - first_nonopt = optind; - - /* Skip any additional non-options - and extend the range of non-options previously skipped. */ - - while (optind < argc - && (argv[optind][0] != '-' || argv[optind][1] == '\0')) - optind++; - last_nonopt = optind; - } - - /* The special ARGV-element `--' means premature end of options. - Skip it like a null option, - then exchange with previous non-options as if it were an option, - then skip everything else like a non-option. */ - - if (optind != argc && !strcmp (argv[optind], "--")) - { - optind++; - - if (first_nonopt != last_nonopt && last_nonopt != optind) - exchange ((char **) argv); - else if (first_nonopt == last_nonopt) - first_nonopt = optind; - last_nonopt = argc; - - optind = argc; - } - - /* If we have done all the ARGV-elements, stop the scan - and back over any non-options that we skipped and permuted. */ - - if (optind == argc) - { - /* Set the next-arg-index to point at the non-options - that we previously skipped, so the caller will digest them. */ - if (first_nonopt != last_nonopt) - optind = first_nonopt; - return EOF; - } - - /* If we have come to a non-option and did not permute it, - either stop the scan or describe it to the caller and pass it by. */ - - if ((argv[optind][0] != '-' || argv[optind][1] == '\0')) - { - if (ordering == REQUIRE_ORDER) - return EOF; - optarg = argv[optind++]; - return 1; - } - - /* We have found another option-ARGV-element. - Skip the initial punctuation. */ - - nextchar = (argv[optind] + 1 - + (longopts != NULL && argv[optind][1] == '-')); - } - - /* Decode the current option-ARGV-element. */ - - /* Check whether the ARGV-element is a long option. - - If long_only and the ARGV-element has the form "-f", where f is - a valid short option, don't consider it an abbreviated form of - a long option that starts with f. Otherwise there would be no - way to give the -f short option. - - On the other hand, if there's a long option "fubar" and - the ARGV-element is "-fu", do consider that an abbreviation of - the long option, just like "--fu", and not "-f" with arg "u". - - This distinction seems to be the most useful approach. */ - - if (longopts != NULL - && (argv[optind][1] == '-' - || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) - { - char *nameend; - const struct option *p; - const struct option *pfound = NULL; - int exact = 0; - int ambig = 0; - int indfound=0; /* Keep gcc happy */ - int option_index; - - for (nameend = nextchar; *nameend && *nameend != '='; nameend++) - /* Do nothing. */ ; - - /* Test all long options for either exact match - or abbreviated matches. */ - for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (!strncmp (p->name, nextchar, nameend - nextchar)) - { - if ((size_t) (nameend - nextchar) == (size_t) strlen (p->name)) - { - /* Exact match found. */ - pfound = p; - indfound = option_index; - exact = 1; - break; - } - else if (pfound == NULL) - { - /* First nonexact match found. */ - pfound = p; - indfound = option_index; - } - else - /* Second or later nonexact match found. */ - ambig = 1; - } - - if (ambig && !exact) - { - if (opterr) - fprintf (stderr, "%s: option `%s' is ambiguous\n", - argv[0], argv[optind]); - nextchar += strlen (nextchar); - optind++; - return '?'; - } - - if (pfound != NULL) - { - option_index = indfound; - optind++; - if (*nameend) - { - /* Don't test has_arg with >, because some C compilers don't - allow it to be used on enums. */ - if (pfound->has_arg) - optarg = nameend + 1; - else - { - if (opterr) - { - if (argv[optind - 1][1] == '-') - /* --option */ - fprintf (stderr, - "%s: option `--%s' doesn't allow an argument\n", - argv[0], pfound->name); - else - /* +option or -option */ - fprintf (stderr, - "%s: option `%c%s' doesn't allow an argument\n", - argv[0], argv[optind - 1][0], pfound->name); - } - nextchar += strlen (nextchar); - return '?'; - } - } - else if (pfound->has_arg == 1) - { - if (optind < argc) - optarg = argv[optind++]; - else - { - if (opterr) - fprintf (stderr, "%s: option `%s' requires an argument\n", - argv[0], argv[optind - 1]); - nextchar += strlen (nextchar); - return optstring[0] == ':' ? ':' : '?'; - } - } - nextchar += strlen (nextchar); - if (longind != NULL) - *longind = option_index; - if (pfound->flag) - { - *(pfound->flag) = pfound->val; - return 0; - } - return pfound->val; - } - - /* Can't find it as a long option. If this is not getopt_long_only, - or the option starts with '--' or is not a valid short - option, then it's an error. - Otherwise interpret it as a short option. */ - if (!long_only || argv[optind][1] == '-' - || my_index (optstring, *nextchar) == NULL) - { - if (opterr) - { - if (argv[optind][1] == '-') - /* --option */ - fprintf (stderr, "%s: unrecognized option `--%s'\n", - argv[0], nextchar); - else - /* +option or -option */ - fprintf (stderr, "%s: unrecognized option `%c%s'\n", - argv[0], argv[optind][0], nextchar); - } - nextchar = (char *) ""; - optind++; - return '?'; - } - } - - /* Look at and handle the next short option-character. */ - - { - char c = *nextchar++; - char *temp = my_index (optstring, c); - - /* Increment `optind' when we start to process its last character. */ - if (*nextchar == '\0') - ++optind; - - if (temp == NULL || c == ':') - { - if (opterr) - { - if (posixly_correct) - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); - else - fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c); - } - optopt = c; - return '?'; - } - if (temp[1] == ':') - { - if (temp[2] == ':') - { - /* This is an option that accepts an argument optionally. */ - if (*nextchar != '\0') - { - optarg = nextchar; - optind++; - } - else - optarg = NULL; - nextchar = NULL; - } - else - { - /* This is an option that requires an argument. */ - if (*nextchar != '\0') - { - optarg = nextchar; - /* If we end this ARGV-element by taking the rest as an arg, - we must advance to the next element now. */ - optind++; - } - else if (optind == argc) - { - if (opterr) - { - /* 1003.2 specifies the format of this message. */ - fprintf (stderr, "%s: option requires an argument -- %c\n", - argv[0], c); - } - optopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } - else - /* We already incremented `optind' once; - increment it again when taking next ARGV-elt as argument. */ - optarg = argv[optind++]; - nextchar = NULL; - } - } - return c; - } -} - -#ifdef __EMX__ -int getopt (int argc, char **argv, __const__ char *optstring) -#else -int -getopt (int argc, char *const *argv, const char *optstring) -#endif -{ - return _getopt_internal (argc, argv, optstring, - (const struct option *) 0, - (int *) 0, - 0); -} - -#endif /* _LIBC or not __GNU_LIBRARY__. */ - -#ifdef TEST - -/* Compile with -DTEST to make an executable for use in testing - the above definition of `getopt'. */ - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - - c = getopt (argc, argv, "abc:d:0123456789"); - if (c == EOF) - break; - - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/mysys/getopt1.c b/mysys/getopt1.c deleted file mode 100644 index bff76d6e5b2..00000000000 --- a/mysys/getopt1.c +++ /dev/null @@ -1,170 +0,0 @@ -/* getopt_long and getopt_long_only entry points for GNU getopt. - Copyright (C) 1987, 88, 89, 90, 91, 92, 1993, 1994 - Free Software Foundation, Inc. - -This file is part of the GNU C Library. Its master source is NOT part of -the C library, however. The master source lives in /gd/gnu/lib. - -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. - -The GNU C 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <global.h> -#include "getopt.h" - -#if (!defined (__STDC__) || !__STDC__) && !defined(MSDOS) && !defined(OS2) -/* This is a separate conditional since some stdc systems - reject `defined (const)'. */ -#ifndef const -#define const -#endif -#endif - -#include <stdio.h> - -/* Comment out all this code if we are using the GNU C Library, and are not - actually compiling the library itself. This code is part of the GNU C - Library, but also included in many other GNU distributions. Compiling - and linking in this code is a waste when using the GNU C library - (especially if it is a shared library). Rather than having every GNU - program understand `configure --with-gnu-libc' and omit the object files, - it is simpler to just do this in the source for each such file. */ - -#if defined (_LIBC) || !defined (__GNU_LIBRARY__) - -#ifndef __WIN__ -#include <stdlib.h> -#endif /* __WIN__ */ - -#ifndef NULL -#define NULL 0 -#endif - -int -getopt_long (int argc, char *const *argv, const char *options, const struct option *long_options, int *opt_index) -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 0); -} - -/* Like getopt_long, but '-' as well as '--' can indicate a long option. - If an option that starts with '-' (not '--') doesn't match a long option, - but does match a short option, it is parsed as a short option - instead. */ - -int -getopt_long_only (int argc, char *const *argv, const char *options, const struct option *long_options, int *opt_index) -{ - return _getopt_internal (argc, argv, options, long_options, opt_index, 1); -} - - -#endif /* _LIBC or not __GNU_LIBRARY__. */ - -#ifdef TEST - -#include <stdio.h> - -int -main (argc, argv) - int argc; - char **argv; -{ - int c; - int digit_optind = 0; - - while (1) - { - int this_option_optind = optind ? optind : 1; - int option_index = 0; - static struct option long_options[] = - { - {"add", 1, 0, 0}, - {"append", 0, 0, 0}, - {"delete", 1, 0, 0}, - {"verbose", 0, 0, 0}, - {"create", 0, 0, 0}, - {"file", 1, 0, 0}, - {0, 0, 0, 0} - }; - - c = getopt_long (argc, argv, "abc:d:0123456789", - long_options, &option_index); - if (c == EOF) - break; - - switch (c) - { - case 0: - printf ("option %s", long_options[option_index].name); - if (optarg) - printf (" with arg %s", optarg); - printf ("\n"); - break; - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf ("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf ("option %c\n", c); - break; - - case 'a': - printf ("option a\n"); - break; - - case 'b': - printf ("option b\n"); - break; - - case 'c': - printf ("option c with value `%s'\n", optarg); - break; - - case 'd': - printf ("option d with value `%s'\n", optarg); - break; - - case '?': - break; - - default: - printf ("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) - { - printf ("non-option ARGV-elements: "); - while (optind < argc) - printf ("%s ", argv[optind++]); - printf ("\n"); - } - - exit (0); -} - -#endif /* TEST */ diff --git a/mysys/getvar.c b/mysys/getvar.c deleted file mode 100644 index 90ab599244d..00000000000 --- a/mysys/getvar.c +++ /dev/null @@ -1,112 +0,0 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ - -/* Allow use of the -O variable= option to set long variables */ - -#include "mysys_priv.h" -#include <m_string.h> -#include <m_ctype.h> - - /* set all changeable variables */ - -void set_all_changeable_vars(CHANGEABLE_VAR *vars) -{ - for ( ; vars->name ; vars++) - *vars->varptr= vars->def_value; -} - - -my_bool set_changeable_varval(const char* var, ulong val, - CHANGEABLE_VAR *vars) -{ - char buffer[256]; - sprintf( buffer, "%s=%lu", var, (unsigned long) val ); - return set_changeable_var( buffer, vars ); -} - - -my_bool set_changeable_var(my_string str,CHANGEABLE_VAR *vars) -{ - char endchar; - my_string end; - DBUG_ENTER("set_changeable_var"); - DBUG_PRINT("enter",("%s",str)); - - if (str) - { - if (!(end=strchr(str,'='))) - fprintf(stderr,"Can't find '=' in expression '%s' to option -O\n",str); - else - { - uint length,found_count=0; - CHANGEABLE_VAR *var,*found; - my_string var_end; - const char *name; - longlong num; - - /* Skip end space from variable */ - for (var_end=end ; end > str && isspace(var_end[-1]) ; var_end--) ; - length=(uint) (var_end-str); - /* Skip start space from argument */ - for (end++ ; isspace(*end) ; end++) ; - - for (var=vars,found=0 ; (name=var->name) ; var++) - { - if (!my_casecmp(name,str,length)) - { - found=var; found_count++; - if (!name[length]) - { - found_count=1; - break; - } - } - } - if (found_count == 0) - { - fprintf(stderr,"No variable match for: -O '%s'\n",str); - DBUG_RETURN(1); - } - if (found_count > 1) - { - fprintf(stderr,"Variable prefix '%*s' is not unique\n",length,str); - DBUG_RETURN(1); - } - - num=strtoll(end, (char **)NULL, 10); endchar=strend(end)[-1]; - if (endchar == 'k' || endchar == 'K') - num*=1024; - else if (endchar == 'm' || endchar == 'M') - num*=1024L*1024L; - else if (endchar == 'g' || endchar == 'G') - num*=1024L*1024L*1024L; - else if (!isdigit(endchar)) - { - fprintf(stderr,"Unknown prefix used for variable value '%s'\n",str); - DBUG_RETURN(1); - } - if (num < (longlong) found->min_value) - num=(longlong) found->min_value; - else if (num > 0 && (ulonglong) num > (ulonglong) (ulong) found->max_value) - num=(longlong) (ulong) found->max_value; - num=((num- (longlong) found->sub_size) / (ulonglong) found->block_size); - (*found->varptr)= (long) (num*(ulonglong) found->block_size); - DBUG_RETURN(0); - } - } - DBUG_RETURN(1); -} diff --git a/mysys/hash.c b/mysys/hash.c index 602823e6d43..3afd31a079b 100644 --- a/mysys/hash.c +++ b/mysys/hash.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* The hash functions used for saveing keys */ /* One of key_length or key_length_offset must be given */ @@ -153,9 +152,7 @@ static uint calc_hashnr_caseup(const byte *key,uint length) * * The magic is in the interesting relationship between the special prime * 16777619 (2^24 + 403) and 2^32 and 2^8. - * - * This hash produces the fewest collisions of any function that we've seen so - * far, and works well on both numbers and strings. + * This works well on both numbers and strings. */ uint calc_hashnr(const byte *key, uint len) @@ -522,8 +519,8 @@ my_bool hash_update(HASH *hash,byte *record,byte *old_key,uint old_key_length) /* Search after record with key */ idx=hash_mask((*hash->calc_hashnr)(old_key,(old_key_length ? - old_key_length : - hash->key_length)), + old_key_length : + hash->key_length)), blength,records); new_index=hash_mask(rec_hashnr(hash,record),blength,records); if (idx == new_index) @@ -583,6 +580,18 @@ byte *hash_element(HASH *hash,uint idx) } +/* + Replace old row with new row. This should only be used when key + isn't changed +*/ + +void hash_replace(HASH *hash, uint idx, byte *new_row) +{ + if (idx != NO_RECORD) /* Safety */ + dynamic_element(&hash->array,idx,HASH_LINK*)->data=new_row; +} + + #ifndef DBUG_OFF my_bool hash_check(HASH *hash) diff --git a/mysys/list.c b/mysys/list.c index c9c863d4cc2..ac9e1b979a0 100644 --- a/mysys/list.c +++ b/mysys/list.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Code for handling dubble-linked lists in C diff --git a/mysys/make-conf.c b/mysys/make-conf.c index 9db766574e2..404299e1726 100644 --- a/mysys/make-conf.c +++ b/mysys/make-conf.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* make-conf.c * make a charset .conf file out of a ctype-charset.c file. @@ -34,7 +33,7 @@ #define CH_INCLUDE STRINGIZE(CH_SRC) /* aaaah, that's better */ -#include <my_global.h> +#include <my_my_global.h> #include CH_INCLUDE #include <stdio.h> diff --git a/mysys/md5.c b/mysys/md5.c new file mode 100644 index 00000000000..5de95288141 --- /dev/null +++ b/mysys/md5.c @@ -0,0 +1,369 @@ +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm + */ + +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +rights reserved. + +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD5 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. + +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD5 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. + +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. + +These notices must be retained in any copies of any part of this +documentation and/or software. +*/ + +/* + Changes by Monty: + Replace of MD5_memset and MD5_memcpy with memset & memcpy +*/ + +#include <my_global.h> +#include <m_string.h> +#include "md5.h" + +/* Constants for MD5Transform routine. */ + +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + + +static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); +static void Encode PROTO_LIST + ((unsigned char *, UINT4 *, unsigned int)); +static void Decode PROTO_LIST + ((UINT4 *, unsigned char *, unsigned int)); +#ifdef OLD_CODE +static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int)); +static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int)); +#else +#define MD5_memcpy(A,B,C) memcpy((char*) (A),(char*) (B), (C)) +#define MD5_memset(A,B,C) memset((char*) (A),(B), (C)) +#endif + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. + */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* ROTATE_LEFT rotates x left n bits. + */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. +Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } + +/* MD5 initialization. Begins an MD5 operation, writing a new context. + */ +void my_MD5Init (my_MD5_CTX *context) /* context */ +{ + context->count[0] = context->count[1] = 0; + /* Load magic initialization constants. +*/ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* MD5 block update operation. Continues an MD5 message-digest + operation, processing another message block, and updating the + context. + */ + +void my_MD5Update ( +my_MD5_CTX *context, /* context */ +unsigned char *input, /* input block */ +unsigned int inputLen) /* length of input block */ +{ + unsigned int i, idx, partLen; + + /* Compute number of bytes mod 64 */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); + + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - idx; + + /* Transform as many times as possible. +*/ + if (inputLen >= partLen) { + MD5_memcpy((POINTER)&context->buffer[idx], (POINTER)input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform (context->state, &input[i]); + + idx = 0; + } + else + i = 0; + + /* Buffer remaining input */ + MD5_memcpy((POINTER)&context->buffer[idx], (POINTER)&input[i], + inputLen-i); +} + +/* MD5 finalization. Ends an MD5 message-digest operation, writing the + the message digest and zeroizing the context. + */ + +void my_MD5Final ( +unsigned char digest[16], /* message digest */ +my_MD5_CTX *context) /* context */ +{ + unsigned char bits[8]; + unsigned int idx, padLen; + + /* Save number of bits */ + Encode (bits, context->count, 8); + + /* Pad out to 56 mod 64. +*/ + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); + my_MD5Update (context, PADDING, padLen); + + /* Append length (before padding) */ + my_MD5Update (context, bits, 8); + + /* Store state in digest */ + Encode (digest, context->state, 16); + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)context, 0, sizeof (*context)); +} + +/* MD5 basic transformation. Transforms state based on block. + */ +static void MD5Transform ( +UINT4 state[4], +unsigned char block[64]) +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode (x, block, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. +*/ + MD5_memset ((POINTER)x, 0, sizeof (x)); +} + +/* Encodes input (UINT4) into output (unsigned char). Assumes len is + a multiple of 4. + */ +static void Encode ( +unsigned char *output, +UINT4 *input, +unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + + +/* Decodes input (unsigned char) into output (UINT4). Assumes len is + a multiple of 4. + */ +static void Decode ( +UINT4 *output, +unsigned char *input, +unsigned int len) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); +} + +/* Note: Replace "for loop" with standard memcpy if possible. + */ + +#ifndef MD5_memcpy +static void MD5_memcpy (output, input, len) +POINTER output; +POINTER input; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + output[i] = input[i]; +} +#endif + +/* Note: Replace "for loop" with standard memset if possible. + */ + +#ifndef MD5_memset +static void MD5_memset (output, value, len) +POINTER output; +int value; +unsigned int len; +{ + unsigned int i; + + for (i = 0; i < len; i++) + ((char *)output)[i] = (char)value; +} +#endif diff --git a/mysys/mf_brkhant.c b/mysys/mf_brkhant.c index debf5d9a712..4180bd6df66 100644 --- a/mysys/mf_brkhant.c +++ b/mysys/mf_brkhant.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Dont let the user break when you are doing something important */ /* Remembers if it got 'SIGINT' and executes it on allow_break */ diff --git a/mysys/mf_cache.c b/mysys/mf_cache.c index 85b6b19b328..2c5d8658625 100644 --- a/mysys/mf_cache.c +++ b/mysys/mf_cache.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB +/* Copyright (C) 2000 MySQL AB - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This library is distributed in the hope that it will be useful, + This program 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 - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Library 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 */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Open a temporary file and cache it with io_cache. Delete it on close */ diff --git a/mysys/mf_casecnv.c b/mysys/mf_casecnv.c index 125f54b2594..c037cbd0f16 100644 --- a/mysys/mf_casecnv.c +++ b/mysys/mf_casecnv.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Functions to convert to lover_case and to upper_case in scandinavia. @@ -25,9 +24,25 @@ #include "mysys_priv.h" #include <m_ctype.h> +#ifndef SCO #include <m_string.h> +#endif - /* string to uppercase */ +/* + Upcase string + + SYNOPSIS + str IN/OUT String to upcase + + RETURN VALUE + none + DESCRIPTION + Function changes input parameter so all chars it consist from + are replaced with matching one in upper case. + String should be writable with exception read-only empty string + constant is handled correctly. +*/ + void caseup_str(my_string str) { @@ -35,7 +50,7 @@ void caseup_str(my_string str) if (use_mb(default_charset_info)) { register uint32 l; - register char *end=str+(uint) strlen(str); + register char *end=str+strlen(str); while (*str) { if ((l=my_ismbchar(default_charset_info, str,end))) str+=l; @@ -44,11 +59,29 @@ void caseup_str(my_string str) } else #endif - while ((*str = toupper(*str)) != 0) + while (*str!=0) /* iterate till the end of string */ + { + *str= toupper(*str); str++; + } } /* caseup_str */ - /* string to lowercase */ + +/* + Downcase string + + SYNOPSIS + str IN/OUT String to downcase + + RETURN VALUE + none + DESCRIPTION + Function changes input parameter so all chars it consist from + are replaced with matching one in lower case. + String should be writable with exception read-only empty string + constant is handled correctly. +*/ + void casedn_str(my_string str) { @@ -56,7 +89,7 @@ void casedn_str(my_string str) if (use_mb(default_charset_info)) { register uint32 l; - register char *end=str+(uint) strlen(str); + register char *end=str+strlen(str); while (*str) { if ((l=my_ismbchar(default_charset_info, str,end))) str+=l; @@ -65,8 +98,11 @@ void casedn_str(my_string str) } else #endif - while ((*str= tolower(*str)) != 0) + while (*str!=0) /* iterate till the end of string */ + { + *str= tolower(*str); str++; + } } /* casedn_str */ @@ -155,7 +191,7 @@ int my_strcasecmp(const char *s, const char *t) if (use_mb(default_charset_info)) { register uint32 l; - register const char *end=s+(uint) strlen(s); + register const char *end=s+strlen(s); while (s<end) { if ((l=my_ismbchar(default_charset_info, s,end))) diff --git a/mysys/mf_dirname.c b/mysys/mf_dirname.c index 399082a238b..6523abcdb12 100644 --- a/mysys/mf_dirname.c +++ b/mysys/mf_dirname.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> @@ -50,57 +49,70 @@ uint dirname_part(my_string to, const char *name) DBUG_PRINT("enter",("'%s'",name)); length=dirname_length(name); - (void) strmake(to,(char*) name,min(length,FN_REFLEN-2)); - convert_dirname(to); /* Convert chars */ + convert_dirname(to, name, name+length); DBUG_RETURN(length); } /* dirname */ - /* convert dirname to use under this system */ - /* If MSDOS converts '/' to '\' */ - /* If VMS converts '<' to '[' and '>' to ']' */ - /* Adds a '/' to end if there isn't one and the last isn't a dev_char */ - /* ARGSUSED */ + /* + Convert directory name to use under this system + If MSDOS converts '/' to '\' + If VMS converts '<' to '[' and '>' to ']' + Adds a FN_LIBCHAR to end if the result string if there isn't one + and the last isn't dev_char. + Copies data from 'from' until ASCII(0) for until from == from_end + If you want to use the whole 'from' string, just send NullS as the + last argument. + If the result string is larger than FN_REFLEN -1, then it's cut. + + Returns pointer to end \0 + */ #ifndef FN_DEVCHAR #define FN_DEVCHAR '\0' /* For easier code */ #endif -char *convert_dirname(my_string to) +char *convert_dirname(char *to, const char *from, const char *from_end) { - reg1 char *pos; -#ifdef FN_UPPER_CASE - caseup_str(to); -#endif -#ifdef FN_LOWER_CASE - casedn_str(to); -#endif -#if FN_LIBCHAR != '/' - { - pos=to-1; /* Change from '/' */ - while ((pos=strchr(pos+1,'/')) != 0) - *pos=FN_LIBCHAR; - } -#endif -#ifdef FN_C_BEFORE_DIR_2 + char *to_org=to; + + /* We use -2 here, becasue we need place for the last FN_LIBCHAR */ + if (!from_end || (from_end - from) > FN_REFLEN-2) + from_end=from+FN_REFLEN -2; + +#if FN_LIBCHAR != '/' || defined(FN_C_BEFORE_DIR_2) { - for (pos=to ; *pos ; pos++) + for (; *from && from != from_end; from++) { - if (*pos == FN_C_BEFORE_DIR_2) - *pos=FN_C_BEFORE_DIR; - if (*pos == FN_C_AFTER_DIR_2) - *pos=FN_C_AFTER_DIR; + if (*from == '/') + *to++= FN_LIBCHAR; +#ifdef FN_C_BEFORE_DIR_2 + else if (*from == FN_C_BEFORE_DIR_2) + *to++= FN_C_BEFORE_DIR; + else if (*from == FN_C_AFTER_DIR_2) + *to++= FN_C_AFTER_DIR; +#endif + else + *to++= *from; } + *to=0; } #else - { /* Append FN_LIBCHAR if not there */ - pos=strend(to); - if (pos != to && (pos[-1] != FN_LIBCHAR && pos[-1] != FN_DEVCHAR)) - { - *pos++=FN_LIBCHAR; - *pos=0; - } + /* This is ok even if to == from, becasue we need to cut the string */ + to= strmake(to, from, (uint) (from_end-from)); +#endif + + /* Add FN_LIBCHAR to the end of directory path */ + if (to != to_org && (to[-1] != FN_LIBCHAR && to[-1] != FN_DEVCHAR)) + { + *to++=FN_LIBCHAR; + *to=0; } +#ifdef FN_UPPER_CASE + caseup_str(to_org); +#endif +#ifdef FN_LOWER_CASE + casedn_str(to_org); #endif - return pos; /* Pointer to end of dir */ + return to; /* Pointer to end of dir */ } /* convert_dirname */ diff --git a/mysys/mf_fn_ext.c b/mysys/mf_fn_ext.c index 6a9b9d18341..9c86a8072ef 100644 --- a/mysys/mf_fn_ext.c +++ b/mysys/mf_fn_ext.c @@ -1,30 +1,38 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Returnerar en pekare till filnamnets extension. */ #include "mysys_priv.h" #include <m_string.h> - /* Return a pointerto the extension of the filename - The pointer points at the extension character (normally '.')) - If there isn't any extension, the pointer points at the end - NULL of the filename - */ +/* + Return a pointer to the extension of the filename. + + SYNOPSIS + fn_ext() + name Name of file + + DESCRIPTION + The extension is defined as everything after the first extension character + (normally '.') after the directory name. + + RETURN VALUES + Pointer to to the extension character. If there isn't any extension, + points at the end ASCII(0) of the filename. +*/ my_string fn_ext(const char *name) { @@ -41,6 +49,6 @@ my_string fn_ext(const char *name) if (!(gpos=strrchr(name,FNLIBCHAR))) gpos=name; #endif - pos=strrchr(gpos,FN_EXTCHAR); + pos=strchr(gpos,FN_EXTCHAR); DBUG_RETURN (pos ? pos : strend(gpos)); } /* fn_ext */ diff --git a/mysys/mf_format.c b/mysys/mf_format.c index 7dac46cf0d0..ab1904da162 100644 --- a/mysys/mf_format.c +++ b/mysys/mf_format.c @@ -1,94 +1,84 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> -#ifdef HAVE_REALPATH -#include <sys/param.h> -#include <sys/stat.h> -#endif - /* format a filename with replace of library and extension */ - /* params to and name may be identicall */ - /* function doesn't change name if name != to */ - /* Flag may be: 1 replace filenames library with 'dsk' */ - /* 2 replace extension with 'form' */ - /* 4 Unpack filename (replace ~ with home) */ - /* 8 Pack filename as short as possibly */ - /* 16 Resolve symbolic links for filename */ - /* 32 Resolve filename to full path */ - /* 64 Return NULL if too long path */ + /* + Formats a filename with possible replace of directory of extension + Function can handle the case where 'to' == 'name' + For a description of the flag values, consult my_sys.h + The arguments should be in unix format. + */ -#ifdef SCO -#define BUFF_LEN 4097 -#else -#ifdef MAXPATHLEN -#define BUFF_LEN MAXPATHLEN -#else -#define BUFF_LEN FN_LEN -#endif -#endif -my_string fn_format(my_string to, const char *name, const char *dsk, - const char *form, int flag) +my_string fn_format(my_string to, const char *name, const char *dir, + const char *extension, uint flag) { reg1 uint length; - char dev[FN_REFLEN], buff[BUFF_LEN], *pos, *startpos; + char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos; const char *ext; DBUG_ENTER("fn_format"); - DBUG_PRINT("enter",("name: %s dsk: %s form: %s flag: %d", - name,dsk,form,flag)); + DBUG_PRINT("enter",("name: %s dir: %s extension: %s flag: %d", + name,dir,extension,flag)); - /* Kopiera & skippa enheten */ + /* Copy and skip directory */ name+=(length=dirname_part(dev,(startpos=(my_string) name))); - if (length == 0 || flag & 1) + if (length == 0 || (flag & MY_REPLACE_DIR)) { - (void) strmake(dev,dsk, sizeof(dev) - 2); - /* Use given directory */ - convert_dirname(dev); /* Fix to this OS */ + /* Use given directory */ + convert_dirname(dev,dir,NullS); /* Fix to this OS */ } - if (flag & 8) + else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(name)) + { + /* Put 'dir' before the given path */ + strmake(buff,dev,sizeof(buff)-1); + pos=convert_dirname(dev,dir,NullS); + strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev)); + } + + if (flag & MY_PACK_FILENAME) pack_dirname(dev,dev); /* Put in ./.. and ~/.. */ - if (flag & 4) + if (flag & MY_UNPACK_FILENAME) (void) unpack_dirname(dev,dev); /* Replace ~/.. with dir */ - if ((pos=(char*)strchr(name,FN_EXTCHAR)) != NullS) + if ((pos= (char*) strchr(name,FN_EXTCHAR)) != NullS) { - if ((flag & 2) == 0) /* Skall vi byta extension ? */ + if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */ { - length=strlength(name); /* Old extension */ + length=strlength(name); /* Use old extension */ ext = ""; } else { length=(uint) (pos-(char*) name); /* Change extension */ - ext= form; + ext= extension; } } else { - length=strlength(name); /* Har ingen ext- tag nya */ - ext=form; + length=strlength(name); /* No ext, use the now one */ + ext=extension; } if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN ) - { /* To long path, return original */ + { + /* To long path, return original or NULL */ uint tmp_length; - if (flag & 64) - return 0; + if (flag & MY_SAFE_PATH) + return NullS; tmp_length=strlength(startpos); DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %d",dev,ext,length)); (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1)); @@ -109,18 +99,18 @@ my_string fn_format(my_string to, const char *name, const char *dsk, #endif (void) strmov(pos,ext); /* Don't convert extension */ } - /* Purify gives a lot of UMR errors when using realpath */ -#if defined(HAVE_REALPATH) && !defined(HAVE_purify) && !defined(HAVE_BROKEN_REALPATH) - if (flag & 16) + /* + If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do + realpath if the file is a symbolic link + */ + if (flag & MY_RETURN_REAL_PATH) + (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ? + MY_RESOLVE_LINK: 0)); + else if (flag & MY_RESOLVE_SYMLINKS) { - struct stat stat_buff; - if (flag & 32 || (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode))) - { - if (realpath(to,buff)) - strmake(to,buff,FN_REFLEN-1); - } + strmov(buff,to); + (void) my_readlink(to, buff, MYF(0)); } -#endif DBUG_RETURN (to); } /* fn_format */ diff --git a/mysys/mf_getdate.c b/mysys/mf_getdate.c index f01d1d7633a..189d43e782a 100644 --- a/mysys/mf_getdate.c +++ b/mysys/mf_getdate.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Get date in a printable form: yyyy-mm-dd hh:mm:ss */ diff --git a/mysys/mf_iocache.c b/mysys/mf_iocache.c index 0d1c227c2b2..0f35ee048bb 100644 --- a/mysys/mf_iocache.c +++ b/mysys/mf_iocache.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Cashing of files with only does (sequential) read or writes of fixed- @@ -23,12 +22,30 @@ Possibly use of asyncronic io. macros for read and writes for faster io. Used instead of FILE when reading or writing whole files. - This will make mf_rec_cache obsolete. + This code makes mf_rec_cache obsolete (currently only used by ISAM) One can change info->pos_in_file to a higher value to skip bytes in file if - also info->rc_pos is set to info->rc_end. + also info->read_pos is set to info->read_end. If called through open_cached_file(), then the temporary file will only be created if a write exeeds the file buffer or if one calls - flush_io_cache(). + flush_io_cache(). + + If one uses SEQ_READ_APPEND, then two buffers are allocated, one for + reading and another for writing. Reads are first done from disk and + then done from the write buffer. This is an efficient way to read + from a log file when one is writing to it at the same time. + For this to work, the file has to be opened in append mode! + Note that when one uses SEQ_READ_APPEND, one MUST write using + my_b_append ! This is needed because we need to lock the mutex + every time we access the write buffer. + +TODO: + When one SEQ_READ_APPEND and we are reading and writing at the same time, + each time the write buffer gets full and it's written to disk, we will + always do a disk read to read a part of the buffer from disk to the + read buffer. + This should be fixed so that when we do a flush_io_cache() and + we have been reading the write buffer, we should transfer the rest of the + write buffer to the read buffer before we start to reuse it. */ #define MAP_TO_USE_RAID @@ -36,10 +53,62 @@ #include <m_string.h> #ifdef HAVE_AIOWAIT #include "mysys_err.h" -#include <errno.h> static void my_aiowait(my_aio_result *result); #endif +#include <assert.h> +#include <errno.h> + +#ifdef THREAD +#define lock_append_buffer(info) \ + pthread_mutex_lock(&(info)->append_buffer_lock) +#define unlock_append_buffer(info) \ + pthread_mutex_unlock(&(info)->append_buffer_lock) +#else +#define lock_append_buffer(info) +#define unlock_append_buffer(info) +#endif +#define IO_ROUND_UP(X) (((X)+IO_SIZE-1) & ~(IO_SIZE-1)) +#define IO_ROUND_DN(X) ( (X) & ~(IO_SIZE-1)) + +static void +init_functions(IO_CACHE* info, enum cache_type type) +{ + switch (type) { + case READ_NET: + /* + Must be initialized by the caller. The problem is that + _my_b_net_read has to be defined in sql directory because of + the dependency on THD, and therefore cannot be visible to + programs that link against mysys but know nothing about THD, such + as myisamchk + */ + break; + case SEQ_READ_APPEND: + info->read_function = _my_b_seq_read; + info->write_function = 0; /* Force a core if used */ + break; + default: + info->read_function = +#ifdef THREAD + info->share ? _my_b_read_r : +#endif + _my_b_read; + info->write_function = _my_b_write; + } + + /* Ensure that my_b_tell() and my_b_bytes_in_cache works */ + if (type == WRITE_CACHE) + { + info->current_pos= &info->write_pos; + info->current_end= &info->write_end; + } + else + { + info->current_pos= &info->read_pos; + info->current_end= &info->read_end; + } +} /* ** if cachesize == 0 then use default cachesize (from s-file) @@ -52,70 +121,97 @@ int init_io_cache(IO_CACHE *info, File file, uint cachesize, pbool use_async_io, myf cache_myflags) { uint min_cache; + my_off_t end_of_file= ~(my_off_t) 0; DBUG_ENTER("init_io_cache"); - DBUG_PRINT("enter",("type: %d pos: %ld",(int) type, (ulong) seek_offset)); + DBUG_PRINT("enter",("cache: %lx type: %d pos: %ld", + (ulong) info, (int) type, (ulong) seek_offset)); + + info->file= file; + info->type=type; + info->pos_in_file= seek_offset; + info->pre_close = info->pre_read = info->post_read = 0; + info->arg = 0; + info->alloced_buffer = 0; + info->buffer=0; + info->seek_not_done= test(file >= 0); +#ifdef THREAD + info->share=0; +#endif - info->file=file; if (!cachesize) if (! (cachesize= my_default_record_cache_size)) DBUG_RETURN(1); /* No cache requested */ min_cache=use_async_io ? IO_SIZE*4 : IO_SIZE*2; - if (type == READ_CACHE) + if (type == READ_CACHE || type == SEQ_READ_APPEND) { /* Assume file isn't growing */ - if (cache_myflags & MY_DONT_CHECK_FILESIZE) - { - cache_myflags &= ~MY_DONT_CHECK_FILESIZE; - } - else + if (!(cache_myflags & MY_DONT_CHECK_FILESIZE)) { - my_off_t file_pos,end_of_file; - if ((file_pos=my_tell(file,MYF(0)) == MY_FILEPOS_ERROR)) - DBUG_RETURN(1); + /* Calculate end of file to not allocate to big buffers */ end_of_file=my_seek(file,0L,MY_SEEK_END,MYF(0)); if (end_of_file < seek_offset) end_of_file=seek_offset; - VOID(my_seek(file,file_pos,MY_SEEK_SET,MYF(0))); + /* Trim cache size if the file is very small */ if ((my_off_t) cachesize > end_of_file-seek_offset+IO_SIZE*2-1) { cachesize=(uint) (end_of_file-seek_offset)+IO_SIZE*2-1; - use_async_io=0; /* No nead to use async */ + use_async_io=0; /* No need to use async */ } } } - - for (;;) + cache_myflags &= ~MY_DONT_CHECK_FILESIZE; + if (type != READ_NET && type != WRITE_NET) { - cachesize=(uint) ((ulong) (cachesize + min_cache-1) & - (ulong) ~(min_cache-1)); - if (cachesize < min_cache) - cachesize = min_cache; - if ((info->buffer= - (byte*) my_malloc(cachesize, - MYF((cache_myflags & ~ MY_WME) | - (cachesize == min_cache ? MY_WME : 0)))) != 0) - break; /* Enough memory found */ - if (cachesize == min_cache) - DBUG_RETURN(2); /* Can't alloc cache */ - cachesize= (uint) ((long) cachesize*3/4); /* Try with less memory */ + /* Retry allocating memory in smaller blocks until we get one */ + for (;;) + { + uint buffer_block; + cachesize=(uint) ((ulong) (cachesize + min_cache-1) & + (ulong) ~(min_cache-1)); + if (cachesize < min_cache) + cachesize = min_cache; + buffer_block = cachesize; + if (type == SEQ_READ_APPEND) + buffer_block *= 2; + if ((info->buffer= + (byte*) my_malloc(buffer_block, + MYF((cache_myflags & ~ MY_WME) | + (cachesize == min_cache ? MY_WME : 0)))) != 0) + { + info->write_buffer=info->buffer; + if (type == SEQ_READ_APPEND) + info->write_buffer = info->buffer + cachesize; + info->alloced_buffer=1; + break; /* Enough memory found */ + } + if (cachesize == min_cache) + DBUG_RETURN(2); /* Can't alloc cache */ + cachesize= (uint) ((long) cachesize*3/4); /* Try with less memory */ + } } - info->pos_in_file=seek_offset; + + DBUG_PRINT("info",("init_io_cache: cachesize = %u",cachesize)); info->read_length=info->buffer_length=cachesize; - info->seek_not_done= test(file >= 0); /* Seek not done */ info->myflags=cache_myflags & ~(MY_NABP | MY_FNABP); - info->rc_request_pos=info->rc_pos=info->buffer; - - if (type == READ_CACHE) - { - info->rc_end=info->buffer; /* Nothing in cache */ - } - else /* type == WRITE_CACHE */ + info->request_pos= info->read_pos= info->write_pos = info->buffer; + if (type == SEQ_READ_APPEND) { - info->rc_end=info->buffer+info->buffer_length- (seek_offset & (IO_SIZE-1)); + info->append_read_pos = info->write_pos = info->write_buffer; + info->write_end = info->write_buffer + info->buffer_length; +#ifdef THREAD + pthread_mutex_init(&info->append_buffer_lock,MY_MUTEX_INIT_FAST); +#endif } - info->end_of_file=MY_FILEPOS_ERROR; /* May be changed by user */ - info->type=type; + + if (type == WRITE_CACHE) + info->write_end= + info->buffer+info->buffer_length- (seek_offset & (IO_SIZE-1)); + else + info->read_end=info->buffer; /* Nothing in cache */ + + /* End_of_file may be changed by user later */ + info->end_of_file= end_of_file; info->error=0; - info->read_function=_my_b_read; + init_functions(info,type); #ifdef HAVE_AIOWAIT if (use_async_io && ! my_disable_async_io) { @@ -128,7 +224,6 @@ int init_io_cache(IO_CACHE *info, File file, uint cachesize, DBUG_RETURN(0); } /* init_io_cache */ - /* Wait until current request is ready */ #ifdef HAVE_AIOWAIT @@ -156,8 +251,13 @@ static void my_aiowait(my_aio_result *result) } #endif - /* Use this to reset cache to start or other type */ - /* Some simple optimizing is done when reinit in current buffer */ + +/* + Use this to reset cache to re-start reading or to change the type + between READ_CACHE <-> WRITE_CACHE + If we are doing a reinit of a cache where we have the start of the file + in the cache, we are reusing this memory without flushing it to disk. +*/ my_bool reinit_io_cache(IO_CACHE *info, enum cache_type type, my_off_t seek_offset, @@ -165,47 +265,76 @@ my_bool reinit_io_cache(IO_CACHE *info, enum cache_type type, pbool clear_cache) { DBUG_ENTER("reinit_io_cache"); + DBUG_PRINT("enter",("cache: %lx type: %d seek_offset: %lu clear_cache: %d", + (ulong) info, type, (ulong) seek_offset, + (int) clear_cache)); - info->seek_not_done= test(info->file >= 0); /* Seek not done */ + /* One can't do reinit with the following types */ + DBUG_ASSERT(type != READ_NET && info->type != READ_NET && + type != WRITE_NET && info->type != WRITE_NET && + type != SEQ_READ_APPEND && info->type != SEQ_READ_APPEND); + + /* If the whole file is in memory, avoid flushing to disk */ if (! clear_cache && seek_offset >= info->pos_in_file && - seek_offset <= info->pos_in_file + - (uint) (info->rc_end - info->rc_request_pos)) - { /* use current buffer */ + seek_offset <= my_b_tell(info)) + { + /* Reuse current buffer without flushing it to disk */ + byte *pos; if (info->type == WRITE_CACHE && type == READ_CACHE) { - info->rc_end=info->rc_pos; + info->read_end=info->write_pos; info->end_of_file=my_b_tell(info); + info->seek_not_done=1; + } + else if (type == WRITE_CACHE) + { + if (info->type == READ_CACHE) + { + info->write_end=info->write_buffer+info->buffer_length; + info->seek_not_done=1; + } + info->end_of_file = ~(my_off_t) 0; } - else if (info->type == READ_CACHE && type == WRITE_CACHE) - info->rc_end=info->buffer+info->buffer_length; - info->rc_pos=info->rc_request_pos+(seek_offset-info->pos_in_file); + pos=info->request_pos+(seek_offset-info->pos_in_file); + if (type == WRITE_CACHE) + info->write_pos=pos; + else + info->read_pos= pos; #ifdef HAVE_AIOWAIT my_aiowait(&info->aio_result); /* Wait for outstanding req */ #endif } else { + /* + If we change from WRITE_CACHE to READ_CACHE, assume that everything + after the current positions should be ignored + */ if (info->type == WRITE_CACHE && type == READ_CACHE) info->end_of_file=my_b_tell(info); - if (flush_io_cache(info)) + /* flush cache if we want to reuse it */ + if (!clear_cache && flush_io_cache(info)) DBUG_RETURN(1); info->pos_in_file=seek_offset; - info->rc_request_pos=info->rc_pos=info->buffer; + /* Better to do always do a seek */ + info->seek_not_done=1; + info->request_pos=info->read_pos=info->write_pos=info->buffer; if (type == READ_CACHE) { - info->rc_end=info->buffer; /* Nothing in cache */ + info->read_end=info->buffer; /* Nothing in cache */ } else { - info->rc_end=info->buffer+info->buffer_length- - (seek_offset & (IO_SIZE-1)); - info->end_of_file=MY_FILEPOS_ERROR; /* May be changed by user */ + info->write_end=(info->buffer + info->buffer_length - + (seek_offset & (IO_SIZE-1))); + info->end_of_file= ~(my_off_t) 0; } } info->type=type; info->error=0; - info->read_function=_my_b_read; + init_functions(info,type); + #ifdef HAVE_AIOWAIT if (use_async_io && ! my_disable_async_io && ((ulong) info->buffer_length < @@ -217,23 +346,34 @@ my_bool reinit_io_cache(IO_CACHE *info, enum cache_type type, info->inited=0; #endif DBUG_RETURN(0); -} /* init_io_cache */ +} /* reinit_io_cache */ - /* Read buffered. Returns 1 if can't read requested characters */ - /* Returns 0 if record read */ +/* + Read buffered. Returns 1 if can't read requested characters + This function is only called from the my_b_read() macro + when there isn't enough characters in the buffer to + satisfy the request. + Returns 0 we succeeded in reading all data +*/ int _my_b_read(register IO_CACHE *info, byte *Buffer, uint Count) { uint length,diff_length,left_length; my_off_t max_length, pos_in_file; + DBUG_ENTER("_my_b_read"); - memcpy(Buffer,info->rc_pos, - (size_t) (left_length=(uint) (info->rc_end-info->rc_pos))); - Buffer+=left_length; - Count-=left_length; - pos_in_file=info->pos_in_file+(uint) (info->rc_end - info->buffer); + if ((left_length=(uint) (info->read_end-info->read_pos))) + { + DBUG_ASSERT(Count >= left_length); /* User is not using my_b_read() */ + memcpy(Buffer,info->read_pos, (size_t) (left_length)); + Buffer+=left_length; + Count-=left_length; + } + + /* pos_in_file always point on where info->buffer was read */ + pos_in_file=info->pos_in_file+(uint) (info->read_end - info->buffer); if (info->seek_not_done) { /* File touched, do seek */ VOID(my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0))); @@ -246,15 +386,15 @@ int _my_b_read(register IO_CACHE *info, byte *Buffer, uint Count) if (info->end_of_file == pos_in_file) { /* End of file */ info->error=(int) left_length; - return 1; + DBUG_RETURN(1); } length=(Count & (uint) ~(IO_SIZE-1))-diff_length; if ((read_length=my_read(info->file,Buffer,(uint) length,info->myflags)) != (uint) length) { - info->error= read_length == (uint) -1 ? -1 : - (int) (read_length+left_length); - return 1; + info->error= (read_length == (uint) -1 ? -1 : + (int) (read_length+left_length)); + DBUG_RETURN(1); } Count-=length; Buffer+=length; @@ -262,16 +402,17 @@ int _my_b_read(register IO_CACHE *info, byte *Buffer, uint Count) left_length+=length; diff_length=0; } - max_length=info->end_of_file - pos_in_file; - if (max_length > info->read_length-diff_length) - max_length=info->read_length-diff_length; + max_length=info->read_length-diff_length; + if (info->type != READ_FIFO && + max_length > (info->end_of_file - pos_in_file)) + max_length = info->end_of_file - pos_in_file; if (!max_length) { if (Count) { info->error= left_length; /* We only got this many char */ - return 1; + DBUG_RETURN(1); } length=0; /* Didn't read any chars */ } @@ -281,14 +422,301 @@ int _my_b_read(register IO_CACHE *info, byte *Buffer, uint Count) { if (length != (uint) -1) memcpy(Buffer,info->buffer,(size_t) length); + info->pos_in_file= pos_in_file; info->error= length == (uint) -1 ? -1 : (int) (length+left_length); + info->read_pos=info->read_end=info->buffer; + DBUG_RETURN(1); + } + info->read_pos=info->buffer+Count; + info->read_end=info->buffer+length; + info->pos_in_file=pos_in_file; + memcpy(Buffer,info->buffer,(size_t) Count); + DBUG_RETURN(0); +} + +#ifdef THREAD +/* Prepare IO_CACHE for shared use */ +void init_io_cache_share(IO_CACHE *info, IO_CACHE_SHARE *s, uint num_threads) +{ + DBUG_ASSERT(info->type == READ_CACHE); + pthread_mutex_init(&s->mutex, MY_MUTEX_INIT_FAST); + pthread_cond_init (&s->cond, 0); + s->total=s->count=num_threads-1; + s->active=0; + info->share=s; + info->read_function=_my_b_read_r; + info->current_pos= info->current_end= 0; +} + +/* + Remove a thread from shared access to IO_CACHE + Every thread should do that on exit for not + to deadlock other threads +*/ +void remove_io_thread(IO_CACHE *info) +{ + IO_CACHE_SHARE *s=info->share; + + pthread_mutex_lock(&s->mutex); + s->total--; + if (! s->count--) + pthread_cond_signal(&s->cond); + pthread_mutex_unlock(&s->mutex); +} + +static int lock_io_cache(IO_CACHE *info, my_off_t pos) +{ + int total; + IO_CACHE_SHARE *s=info->share; + + pthread_mutex_lock(&s->mutex); + if (!s->count) + { + s->count=s->total; + return 1; + } + + total=s->total; + s->count--; + while (!s->active || s->active->pos_in_file < pos) + pthread_cond_wait(&s->cond, &s->mutex); + + if (s->total < total) return 1; + + pthread_mutex_unlock(&s->mutex); + return 0; +} + +static void unlock_io_cache(IO_CACHE *info) +{ + pthread_cond_broadcast(&info->share->cond); + pthread_mutex_unlock(&info->share->mutex); +} + + +/* + Read from IO_CACHE when it is shared between several threads. + It works as follows: when a thread tries to read from a file + (that is, after using all the data from the (shared) buffer), + it just hangs on lock_io_cache(), wating for other threads. + When the very last thread attempts a read, lock_io_cache() + returns 1, the thread does actual IO and unlock_io_cache(), + which signals all the waiting threads that data is in the buffer. +*/ + +int _my_b_read_r(register IO_CACHE *info, byte *Buffer, uint Count) +{ + my_off_t pos_in_file; + uint length,diff_length,read_len; + DBUG_ENTER("_my_b_read_r"); + + if ((read_len=(uint) (info->read_end-info->read_pos))) + { + DBUG_ASSERT(Count >= read_len); /* User is not using my_b_read() */ + memcpy(Buffer,info->read_pos, (size_t) (read_len)); + Buffer+=read_len; + Count-=read_len; + } + while (Count) + { + int cnt, len; + + pos_in_file= info->pos_in_file + (uint)(info->read_end - info->buffer); + diff_length= (uint) (pos_in_file & (IO_SIZE-1)); + length=IO_ROUND_UP(Count+diff_length)-diff_length; + length=(length <= info->read_length) ? + length + IO_ROUND_DN(info->read_length - length) : + length - IO_ROUND_UP(length - info->read_length) ; + if (info->type != READ_FIFO && + (length > (uint) (info->end_of_file - pos_in_file))) + length= (uint) (info->end_of_file - pos_in_file); + if (length == 0) + { + info->error=(int) read_len; + DBUG_RETURN(1); + } + if (lock_io_cache(info, pos_in_file)) + { + info->share->active=info; + if (info->seek_not_done) /* File touched, do seek */ + VOID(my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0))); + len=(int)my_read(info->file,info->buffer, length, info->myflags); + info->read_end=info->buffer + (len == -1 ? 0 : len); + info->error=(len == (int)length ? 0 : len); + info->pos_in_file=pos_in_file; + unlock_io_cache(info); + } + else + { + info->error= info->share->active->error; + info->read_end= info->share->active->read_end; + info->pos_in_file= info->share->active->pos_in_file; + len= (info->error == -1 ? -1 : info->read_end-info->buffer); + } + info->read_pos=info->buffer; + info->seek_not_done=0; + if (len <= 0) + { + info->error=(int) read_len; + DBUG_RETURN(1); + } + cnt=((uint) len > Count) ? (int) Count : len; + memcpy(Buffer,info->read_pos, (size_t)cnt); + Count -=cnt; + Buffer+=cnt; + read_len+=cnt; + info->read_pos+=cnt; } - info->rc_pos=info->buffer+Count; - info->rc_end=info->buffer+length; + DBUG_RETURN(0); +} +#endif + + +/* + Do sequential read from the SEQ_READ_APPEND cache + we do this in three stages: + - first read from info->buffer + - then if there are still data to read, try the file descriptor + - afterwards, if there are still data to read, try append buffer +*/ + +int _my_b_seq_read(register IO_CACHE *info, byte *Buffer, uint Count) +{ + uint length,diff_length,left_length,save_count; + my_off_t max_length, pos_in_file; + save_count=Count; + + /* first, read the regular buffer */ + if ((left_length=(uint) (info->read_end-info->read_pos))) + { + DBUG_ASSERT(Count > left_length); /* User is not using my_b_read() */ + memcpy(Buffer,info->read_pos, (size_t) (left_length)); + Buffer+=left_length; + Count-=left_length; + } + lock_append_buffer(info); + + /* pos_in_file always point on where info->buffer was read */ + if ((pos_in_file=info->pos_in_file+(uint) (info->read_end - info->buffer)) >= + info->end_of_file) + goto read_append_buffer; + + /* + With read-append cache we must always do a seek before we read, + because the write could have moved the file pointer astray + */ + VOID(my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0))); + info->seek_not_done=0; + + diff_length=(uint) (pos_in_file & (IO_SIZE-1)); + + /* now the second stage begins - read from file descriptor */ + if (Count >= (uint) (IO_SIZE+(IO_SIZE-diff_length))) + { /* Fill first intern buffer */ + uint read_length; + + length=(Count & (uint) ~(IO_SIZE-1))-diff_length; + if ((read_length=my_read(info->file,Buffer,(uint) length,info->myflags)) == + (uint)-1) + { + info->error= -1; + unlock_append_buffer(info); + return 1; + } + Count-=read_length; + Buffer+=read_length; + pos_in_file+=read_length; + + if (read_length != (uint) length) + { + /* + We only got part of data; Read the rest of the data from the + write buffer + */ + goto read_append_buffer; + } + left_length+=length; + diff_length=0; + } + + max_length=info->read_length-diff_length; + if (max_length > (info->end_of_file - pos_in_file)) + max_length = info->end_of_file - pos_in_file; + if (!max_length) + { + if (Count) + goto read_append_buffer; + length=0; /* Didn't read any more chars */ + } + else + { + length=my_read(info->file,info->buffer,(uint) max_length, + info->myflags); + if (length == (uint) -1) + { + info->error= -1; + unlock_append_buffer(info); + return 1; + } + if (length < Count) + { + memcpy(Buffer,info->buffer,(size_t) length); + Count -= length; + Buffer += length; + + /* + added the line below to make + DBUG_ASSERT(pos_in_file==info->end_of_file) pass. + otherwise this does not appear to be needed + */ + pos_in_file += length; + goto read_append_buffer; + } + } + unlock_append_buffer(info); + info->read_pos=info->buffer+Count; + info->read_end=info->buffer+length; info->pos_in_file=pos_in_file; memcpy(Buffer,info->buffer,(size_t) Count); return 0; + +read_append_buffer: + + /* + Read data from the current write buffer. + Count should never be == 0 here (The code will work even if count is 0) + */ + + { + /* First copy the data to Count */ + uint len_in_buff = (uint) (info->write_pos - info->append_read_pos); + uint copy_len; + uint transfer_len; + + DBUG_ASSERT(info->append_read_pos <= info->write_pos); + /* + TODO: figure out if the assert below is needed or correct. + */ + DBUG_ASSERT(pos_in_file == info->end_of_file); + copy_len=min(Count, len_in_buff); + memcpy(Buffer, info->append_read_pos, copy_len); + info->append_read_pos += copy_len; + Count -= copy_len; + if (Count) + info->error = save_count - Count; + + /* Fill read buffer with data from write buffer */ + memcpy(info->buffer, info->append_read_pos, + (size_t) (transfer_len=len_in_buff - copy_len)); + info->read_pos= info->buffer; + info->read_end= info->buffer+transfer_len; + info->append_read_pos=info->write_pos; + info->pos_in_file=pos_in_file+copy_len; + info->end_of_file+=len_in_buff; + } + unlock_append_buffer(info); + return Count ? 1 : 0; } @@ -301,8 +729,8 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) my_off_t next_pos_in_file; byte *read_buffer; - memcpy(Buffer,info->rc_pos, - (size_t) (left_length=(uint) (info->rc_end-info->rc_pos))); + memcpy(Buffer,info->read_pos, + (size_t) (left_length=(uint) (info->read_end-info->read_pos))); Buffer+=left_length; org_Count=Count; Count-=left_length; @@ -329,13 +757,13 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) (int) (read_length+left_length)); return(1); } - info->pos_in_file+=(uint) (info->rc_end - info->rc_request_pos); + info->pos_in_file+=(uint) (info->read_end - info->request_pos); - if (info->rc_request_pos != info->buffer) - info->rc_request_pos=info->buffer; + if (info->request_pos != info->buffer) + info->request_pos=info->buffer; else - info->rc_request_pos=info->buffer+info->read_length; - info->rc_pos=info->rc_request_pos; + info->request_pos=info->buffer+info->read_length; + info->read_pos=info->request_pos; next_pos_in_file=info->aio_read_pos+read_length; /* Check if pos_in_file is changed @@ -352,8 +780,8 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) { my_off_t offset= (info->pos_in_file - info->aio_read_pos); info->pos_in_file=info->aio_read_pos; /* Whe are here */ - info->rc_pos=info->rc_request_pos+offset; - read_length-=offset; /* Bytes left from rc_pos */ + info->read_pos=info->request_pos+offset; + read_length-=offset; /* Bytes left from read_pos */ } } #ifndef DBUG_OFF @@ -365,16 +793,16 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) #endif /* Copy found bytes to buffer */ length=min(Count,read_length); - memcpy(Buffer,info->rc_pos,(size_t) length); + memcpy(Buffer,info->read_pos,(size_t) length); Buffer+=length; Count-=length; left_length+=length; - info->rc_end=info->rc_pos+read_length; - info->rc_pos+=length; + info->read_end=info->rc_pos+read_length; + info->read_pos+=length; } else next_pos_in_file=(info->pos_in_file+ (uint) - (info->rc_end - info->rc_request_pos)); + (info->read_end - info->request_pos)); /* If reading large blocks, or first read or read with skipp */ if (Count) @@ -388,13 +816,13 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) read_length=IO_SIZE*2- (uint) (next_pos_in_file & (IO_SIZE-1)); if (Count < read_length) { /* Small block, read to cache */ - if ((read_length=my_read(info->file,info->rc_request_pos, + if ((read_length=my_read(info->file,info->request_pos, read_length, info->myflags)) == (uint) -1) return info->error= -1; use_length=min(Count,read_length); - memcpy(Buffer,info->rc_request_pos,(size_t) use_length); - info->rc_pos=info->rc_request_pos+Count; - info->rc_end=info->rc_request_pos+read_length; + memcpy(Buffer,info->request_pos,(size_t) use_length); + info->read_pos=info->request_pos+Count; + info->read_end=info->request_pos+read_length; info->pos_in_file=next_pos_in_file; /* Start of block in cache */ next_pos_in_file+=read_length; @@ -415,7 +843,7 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) info->error= read_length == (uint) -1 ? -1 : read_length+left_length; return 1; } - info->rc_pos=info->rc_end=info->rc_request_pos; + info->read_pos=info->read_end=info->request_pos; info->pos_in_file=(next_pos_in_file+=Count); } } @@ -426,7 +854,7 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) if (max_length > (my_off_t) info->read_length - diff_length) max_length= (my_off_t) info->read_length - diff_length; - if (info->rc_request_pos != info->buffer) + if (info->request_pos != info->buffer) read_buffer=info->buffer; else read_buffer=info->buffer+info->read_length; @@ -443,13 +871,13 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) my_errno=errno; DBUG_PRINT("error",("got error: %d, aio_result: %d from aioread, async skipped", errno, info->aio_result.result.aio_errno)); - if (info->rc_request_pos != info->buffer) + if (info->request_pos != info->buffer) { - bmove(info->buffer,info->rc_request_pos, - (uint) (info->rc_end - info->rc_pos)); - info->rc_request_pos=info->buffer; - info->rc_pos-=info->read_length; - info->rc_end-=info->read_length; + bmove(info->buffer,info->request_pos, + (uint) (info->read_end - info->read_pos)); + info->request_pos=info->buffer; + info->read_pos-=info->read_length; + info->read_end-=info->read_length; } info->read_length=info->buffer_length; /* Use hole buffer */ info->read_function=_my_b_read; /* Use normal IO_READ next */ @@ -467,8 +895,13 @@ int _my_b_async_read(register IO_CACHE *info, byte *Buffer, uint Count) int _my_b_get(IO_CACHE *info) { byte buff; + IO_CACHE_CALLBACK pre_read,post_read; + if ((pre_read = info->pre_read)) + (*pre_read)(info); if ((*(info)->read_function)(info,&buff,1)) return my_b_EOF; + if ((post_read = info->post_read)) + (*post_read)(info); return (int) (uchar) buff; } @@ -478,11 +911,17 @@ int _my_b_write(register IO_CACHE *info, const byte *Buffer, uint Count) { uint rest_length,length; - rest_length=(uint) (info->rc_end - info->rc_pos); - memcpy(info->rc_pos,Buffer,(size_t) rest_length); + if (info->pos_in_file+info->buffer_length > info->end_of_file) + { + my_errno=errno=EFBIG; + return info->error = -1; + } + + rest_length=(uint) (info->write_end - info->write_pos); + memcpy(info->write_pos,Buffer,(size_t) rest_length); Buffer+=rest_length; Count-=rest_length; - info->rc_pos+=rest_length; + info->write_pos+=rest_length; if (flush_io_cache(info)) return 1; if (Count >= IO_SIZE) @@ -499,8 +938,52 @@ int _my_b_write(register IO_CACHE *info, const byte *Buffer, uint Count) Buffer+=length; info->pos_in_file+=length; } - memcpy(info->rc_pos,Buffer,(size_t) Count); - info->rc_pos+=Count; + memcpy(info->write_pos,Buffer,(size_t) Count); + info->write_pos+=Count; + return 0; +} + + +/* + Append a block to the write buffer. + This is done with the buffer locked to ensure that we don't read from + the write buffer before we are ready with it. +*/ + +int my_b_append(register IO_CACHE *info, const byte *Buffer, uint Count) +{ + uint rest_length,length; + + lock_append_buffer(info); + rest_length=(uint) (info->write_end - info->write_pos); + if (Count <= rest_length) + goto end; + memcpy(info->write_pos,Buffer,(size_t) rest_length); + Buffer+=rest_length; + Count-=rest_length; + info->write_pos+=rest_length; + if (_flush_io_cache(info,0)) + { + unlock_append_buffer(info); + return 1; + } + if (Count >= IO_SIZE) + { /* Fill first intern buffer */ + length=Count & (uint) ~(IO_SIZE-1); + if (my_write(info->file,Buffer,(uint) length,info->myflags | MY_NABP)) + { + unlock_append_buffer(info); + return info->error= -1; + } + Count-=length; + Buffer+=length; + info->end_of_file+=length; + } + +end: + memcpy(info->write_pos,Buffer,(size_t) Count); + info->write_pos+=Count; + unlock_append_buffer(info); return 0; } @@ -530,10 +1013,13 @@ int my_block_write(register IO_CACHE *info, const byte *Buffer, uint Count, Buffer+=length; pos+= length; Count-= length; +#ifndef HAVE_PREAD + info->seek_not_done=1; +#endif } /* Check if we want to write inside the used part of the buffer.*/ - length= (uint) (info->rc_end - info->buffer); + length= (uint) (info->write_end - info->buffer); if (pos < info->pos_in_file + length) { uint offset= (uint) (pos - info->pos_in_file); @@ -544,8 +1030,8 @@ int my_block_write(register IO_CACHE *info, const byte *Buffer, uint Count, Buffer+=length; Count-= length; /* Fix length of buffer if the new data was larger */ - if (info->buffer+length > info->rc_pos) - info->rc_pos=info->buffer+length; + if (info->buffer+length > info->write_pos) + info->write_pos=info->buffer+length; if (!Count) return (error); } @@ -555,46 +1041,90 @@ int my_block_write(register IO_CACHE *info, const byte *Buffer, uint Count, return error; } + /* Flush write cache */ -int flush_io_cache(IO_CACHE *info) +#ifdef THREAD +#define LOCK_APPEND_BUFFER if (need_append_buffer_lock) \ + lock_append_buffer(info); +#define UNLOCK_APPEND_BUFFER if (need_append_buffer_lock) \ + unlock_append_buffer(info); +#else +#define LOCK_APPEND_BUFFER +#define UNLOCK_APPEND_BUFFER +#endif + + +int _flush_io_cache(IO_CACHE *info, int need_append_buffer_lock) { uint length; - DBUG_ENTER("flush_io_cache"); + my_bool append_cache; + my_off_t pos_in_file; + DBUG_ENTER("_flush_io_cache"); + + if (!(append_cache = (info->type == SEQ_READ_APPEND))) + need_append_buffer_lock=0; - if (info->type == WRITE_CACHE) + if (info->type == WRITE_CACHE || append_cache) { if (info->file == -1) { if (real_open_cached_file(info)) DBUG_RETURN((info->error= -1)); } - if (info->rc_pos != info->buffer) + LOCK_APPEND_BUFFER; + + if ((length=(uint) (info->write_pos - info->write_buffer))) { - length=(uint) (info->rc_pos - info->buffer); - if (info->seek_not_done) + pos_in_file=info->pos_in_file; + /* + If we have append cache, we always open the file with + O_APPEND which moves the pos to EOF automatically on every write + */ + if (!append_cache && info->seek_not_done) { /* File touched, do seek */ - if (my_seek(info->file,info->pos_in_file,MY_SEEK_SET,MYF(0)) == + if (my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0)) == MY_FILEPOS_ERROR) + { + UNLOCK_APPEND_BUFFER; DBUG_RETURN((info->error= -1)); - info->seek_not_done=0; + } + if (!append_cache) + info->seek_not_done=0; } - info->rc_pos=info->buffer; - info->pos_in_file+=length; - info->rc_end=(info->buffer+info->buffer_length- - (info->pos_in_file & (IO_SIZE-1))); - if (my_write(info->file,info->buffer,length,info->myflags | MY_NABP)) - DBUG_RETURN((info->error= -1)); - DBUG_RETURN(0); + if (!append_cache) + info->pos_in_file+=length; + info->write_end= (info->write_buffer+info->buffer_length- + ((pos_in_file+length) & (IO_SIZE-1))); + + if (my_write(info->file,info->write_buffer,length, + info->myflags | MY_NABP)) + info->error= -1; + else + info->error= 0; + if (!append_cache) + { + set_if_bigger(info->end_of_file,(pos_in_file+length)); + } + else + { + info->end_of_file+=(info->write_pos-info->append_read_pos); + DBUG_ASSERT(info->end_of_file == my_tell(info->file,MYF(0))); + } + + info->append_read_pos=info->write_pos=info->write_buffer; + UNLOCK_APPEND_BUFFER; + DBUG_RETURN(info->error); } } #ifdef HAVE_AIOWAIT - else + else if (info->type != READ_NET) { - my_aiowait(&info->aio_result); /* Wait for outstanding req */ + my_aiowait(&info->aio_result); /* Wait for outstanding req */ info->inited=0; } #endif + UNLOCK_APPEND_BUFFER; DBUG_RETURN(0); } @@ -602,13 +1132,132 @@ int flush_io_cache(IO_CACHE *info) int end_io_cache(IO_CACHE *info) { int error=0; + IO_CACHE_CALLBACK pre_close; DBUG_ENTER("end_io_cache"); - if (info->buffer) + +#ifdef THREAD + /* + if IO_CACHE is shared between several threads, only one + thread needs to call end_io_cache() - just as init_io_cache() + should be called only once and then memcopy'ed + */ + if (info->share) + { + pthread_cond_destroy (&info->share->cond); + pthread_mutex_destroy(&info->share->mutex); + info->share=0; + } +#endif + + if ((pre_close=info->pre_close)) + (*pre_close)(info); + if (info->alloced_buffer) { + info->alloced_buffer=0; if (info->file != -1) /* File doesn't exist */ error=flush_io_cache(info); my_free((gptr) info->buffer,MYF(MY_WME)); - info->buffer=info->rc_pos=(byte*) 0; + info->buffer=info->read_pos=(byte*) 0; + } + if (info->type == SEQ_READ_APPEND) + { + /* Destroy allocated mutex */ + info->type=0; +#ifdef THREAD + pthread_mutex_destroy(&info->append_buffer_lock); +#endif } DBUG_RETURN(error); } /* end_io_cache */ + + +/********************************************************************** + Testing of MF_IOCACHE +**********************************************************************/ + +#ifdef MAIN + +#include <my_dir.h> + +void die(const char* fmt, ...) +{ + va_list va_args; + va_start(va_args,fmt); + fprintf(stderr,"Error:"); + vfprintf(stderr, fmt,va_args); + fprintf(stderr,", errno=%d\n", errno); + exit(1); +} + +int open_file(const char* fname, IO_CACHE* info, int cache_size) +{ + int fd; + if ((fd=my_open(fname,O_CREAT | O_RDWR,MYF(MY_WME))) < 0) + die("Could not open %s", fname); + if (init_io_cache(info, fd, cache_size, SEQ_READ_APPEND, 0,0,MYF(MY_WME))) + die("failed in init_io_cache()"); + return fd; +} + +void close_file(IO_CACHE* info) +{ + end_io_cache(info); + my_close(info->file, MYF(MY_WME)); +} + +int main(int argc, char** argv) +{ + IO_CACHE sra_cache; /* SEQ_READ_APPEND */ + MY_STAT status; + const char* fname="/tmp/iocache.test"; + int cache_size=16384; + char llstr_buf[22]; + int max_block,total_bytes=0; + int i,num_loops=100,error=0; + char *p; + char* block, *block_end; + MY_INIT(argv[0]); + max_block = cache_size*3; + if (!(block=(char*)my_malloc(max_block,MYF(MY_WME)))) + die("Not enough memory to allocate test block"); + block_end = block + max_block; + for (p = block,i=0; p < block_end;i++) + { + *p++ = (char)i; + } + if (my_stat(fname,&status, MYF(0)) && + my_delete(fname,MYF(MY_WME))) + { + die("Delete of %s failed, aborting", fname); + } + open_file(fname,&sra_cache, cache_size); + for (i = 0; i < num_loops; i++) + { + char buf[4]; + int block_size = abs(rand() % max_block); + int4store(buf, block_size); + if (my_b_append(&sra_cache,buf,4) || + my_b_append(&sra_cache, block, block_size)) + die("write failed"); + total_bytes += 4+block_size; + } + close_file(&sra_cache); + my_free(block,MYF(MY_WME)); + if (!my_stat(fname,&status,MYF(MY_WME))) + die("%s failed to stat, but I had just closed it,\ + wonder how that happened"); + printf("Final size of %s is %s, wrote %d bytes\n",fname, + llstr(status.st_size,llstr_buf), + total_bytes); + my_delete(fname, MYF(MY_WME)); + /* check correctness of tests */ + if (total_bytes != status.st_size) + { + fprintf(stderr,"Not the same number of bytes acutally in file as bytes \ +supposedly written\n"); + error=1; + } + exit(error); + return 0; +} +#endif diff --git a/mysys/mf_iocache2.c b/mysys/mf_iocache2.c index 3e9cc74e0a2..344b7ac2251 100644 --- a/mysys/mf_iocache2.c +++ b/mysys/mf_iocache2.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* More functions to be used with IO_CACHE files @@ -24,58 +23,118 @@ #include <m_string.h> #include <stdarg.h> #include <m_ctype.h> +#include <assert.h> + +my_off_t my_b_append_tell(IO_CACHE* info) +{ + /* + Prevent optimizer from putting res in a register when debugging + we need this to be able to see the value of res when the assert fails + */ + dbug_volatile my_off_t res; + + /* + We need to lock the append buffer mutex to keep flush_io_cache() + from messing with the variables that we need in order to provide the + answer to the question. + */ +#ifdef THREAD + pthread_mutex_lock(&info->append_buffer_lock); +#endif +#ifndef DBUG_OFF + /* + Make sure EOF is where we think it is. Note that we cannot just use + my_tell() because we have a reader thread that could have left the + file offset in a non-EOF location + */ + { + volatile my_off_t save_pos; + save_pos = my_tell(info->file,MYF(0)); + my_seek(info->file,(my_off_t)0,MY_SEEK_END,MYF(0)); + /* + Save the value of my_tell in res so we can see it when studying coredump + */ + DBUG_ASSERT(info->end_of_file - (info->append_read_pos-info->write_buffer) + == (res=my_tell(info->file,MYF(0)))); + my_seek(info->file,save_pos,MY_SEEK_SET,MYF(0)); + } +#endif + res = info->end_of_file + (info->write_pos-info->append_read_pos); +#ifdef THREAD + pthread_mutex_unlock(&info->append_buffer_lock); +#endif + return res; +} /* -** Fix that next read will be made at certain position -** For write cache, make next write happen at a certain position + Make next read happen at the given position + For write cache, make next write happen at the given position */ void my_b_seek(IO_CACHE *info,my_off_t pos) { - my_off_t offset = (pos - info->pos_in_file); + my_off_t offset; DBUG_ENTER("my_b_seek"); DBUG_PRINT("enter",("pos: %lu", (ulong) pos)); - if (info->type == READ_CACHE) + /* + TODO: + Verify that it is OK to do seek in the non-append + area in SEQ_READ_APPEND cache + a) see if this always works + b) see if there is a better way to make it work + */ + if (info->type == SEQ_READ_APPEND) + flush_io_cache(info); + + offset=(pos - info->pos_in_file); + + if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND) { - if ((ulonglong) offset < (ulonglong) (info->rc_end - info->buffer)) + /* TODO: explain why this works if pos < info->pos_in_file */ + if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer)) { /* The read is in the current buffer; Reuse it */ - info->rc_pos = info->buffer + offset; + info->read_pos = info->buffer + offset; DBUG_VOID_RETURN; } else { /* Force a new read on next my_b_read */ - info->rc_pos=info->rc_end=info->buffer; + info->read_pos=info->read_end=info->buffer; } } else if (info->type == WRITE_CACHE) { /* If write is in current buffer, reuse it */ if ((ulonglong) offset < - (ulonglong) (info->rc_end - info->buffer)) + (ulonglong) (info->write_end - info->write_buffer)) { - info->rc_pos = info->buffer + offset; + info->write_pos = info->write_buffer + offset; DBUG_VOID_RETURN; } flush_io_cache(info); - info->rc_end=(info->buffer+info->buffer_length-(pos & (IO_SIZE-1))); + /* Correct buffer end so that we write in increments of IO_SIZE */ + info->write_end=(info->write_buffer+info->buffer_length- + (pos & (IO_SIZE-1))); } info->pos_in_file=pos; info->seek_not_done=1; + DBUG_VOID_RETURN; } + /* -** Fill buffer. Note that this assumes that you have already used -** all characters in the CACHE, independent of the rc_pos value! -** return: 0 on error or EOF (info->error = -1 on error) -** number of characters + Fill buffer. Note that this assumes that you have already used + all characters in the CACHE, independent of the read_pos value! + return: 0 on error or EOF (info->error = -1 on error) + number of characters */ uint my_b_fill(IO_CACHE *info) { - my_off_t pos_in_file=info->pos_in_file+(uint) (info->rc_end - info->buffer); + my_off_t pos_in_file=(info->pos_in_file+ + (uint) (info->read_end - info->buffer)); my_off_t max_length; uint diff_length,length; if (info->seek_not_done) @@ -103,16 +162,18 @@ uint my_b_fill(IO_CACHE *info) info->error= -1; return 0; } - info->rc_pos=info->buffer; - info->rc_end=info->buffer+length; + info->read_pos=info->buffer; + info->read_end=info->buffer+length; info->pos_in_file=pos_in_file; return length; } + /* -** Read a string ended by '\n' into a buffer of 'max_length' size. -** Returns number of characters read, 0 on error. -** last byte is set to '\0' + Read a string ended by '\n' into a buffer of 'max_length' size. + Returns number of characters read, 0 on error. + last byte is set to '\0' + If buffer is full then to[max_length-1] will be set to \0. */ uint my_b_gets(IO_CACHE *info, char *to, uint max_length) @@ -129,11 +190,11 @@ uint my_b_gets(IO_CACHE *info, char *to, uint max_length) char *pos,*end; if (length > max_length) length=max_length; - for (pos=info->rc_pos,end=pos+length ; pos < end ;) + for (pos=info->read_pos,end=pos+length ; pos < end ;) { if ((*to++ = *pos++) == '\n') { - info->rc_pos=pos; + info->read_pos=pos; *to='\0'; return (uint) (to-start); } @@ -141,7 +202,7 @@ uint my_b_gets(IO_CACHE *info, char *to, uint max_length) if (!(max_length-=length)) { /* Found enough charcters; Return found string */ - info->rc_pos=pos; + info->read_pos=pos; *to='\0'; return (uint) (to-start); } @@ -150,6 +211,21 @@ uint my_b_gets(IO_CACHE *info, char *to, uint max_length) } } + +my_off_t my_b_filelength(IO_CACHE *info) +{ + if (info->type == WRITE_CACHE) + { + return my_b_tell(info); + } + else + { + info->seek_not_done=1; + return my_seek(info->file,0L,MY_SEEK_END,MYF(0)); + } +} + + /* Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu" Used for logging in MySQL diff --git a/mysys/mf_keycache.c b/mysys/mf_keycache.c index d5b0a0a056a..6a037f13f05 100644 --- a/mysys/mf_keycache.c +++ b/mysys/mf_keycache.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* This functions is handle keyblock cacheing for NISAM, MISAM and PISAM @@ -53,17 +52,19 @@ typedef struct sec_link { } SEC_LINK; -static uint find_next_bigger_power(uint value); static SEC_LINK *find_key_block(int file,my_off_t filepos,int *error); +static int flush_all_key_blocks(); /* static variables in this file */ static SEC_LINK *_my_block_root,**_my_hash_root, *_my_used_first,*_my_used_last; static int _my_disk_blocks; static uint _my_disk_blocks_used, _my_hash_blocks; +static uint key_cache_shift; ulong _my_blocks_used,_my_blocks_changed; ulong _my_cache_w_requests,_my_cache_write,_my_cache_r_requests, _my_cache_read; +uint key_cache_block_size=DEFAULT_KEYCACHE_BLOCK_SIZE; static byte HUGE_PTR *_my_block_mem; static SEC_LINK *changed_blocks[CHANGED_BLOCKS_HASH]; static SEC_LINK *file_blocks[CHANGED_BLOCKS_HASH]; @@ -76,11 +77,9 @@ static my_bool _my_printed; /* Returns blocks in use */ /* ARGSUSED */ -int init_key_cache(ulong use_mem, - ulong leave_this_much_mem __attribute__((unused))) +int init_key_cache(ulong use_mem) { uint blocks,length; - byte *extra_mem=0; DBUG_ENTER("init_key_cache"); if (key_cache_inited && _my_disk_blocks > 0) @@ -92,28 +91,30 @@ int init_key_cache(ulong use_mem, { key_cache_inited=TRUE; _my_disk_blocks= -1; + key_cache_shift=my_bit_log2(key_cache_block_size); + DBUG_PRINT("info",("key_cache_block_size: %u key_cache_shift: %u", + key_cache_block_size, key_cache_shift)); #ifndef DBUG_OFF _my_printed=0; #endif } - blocks= (uint) (use_mem/(sizeof(SEC_LINK)+sizeof(SEC_LINK*)*5/4+KEYCACHE_BLOCK_SIZE)); + blocks= (uint) (use_mem/(sizeof(SEC_LINK)+sizeof(SEC_LINK*)*5/4+ + key_cache_block_size)); /* No use to have very few blocks */ if (blocks >= 8 && _my_disk_blocks < 0) { -#if !defined(HAVE_ALLOCA) && !defined(THREAD) - if ((extra_mem=my_malloc((uint) leave_this_much_mem,MYF(0))) == 0) - goto err; -#endif for (;;) { - if ((_my_hash_blocks=find_next_bigger_power((uint) blocks)) < blocks*5/4) - _my_hash_blocks<<=1; + /* Set my_hash_blocks to the next bigger 2 power */ + _my_hash_blocks=(uint) 1 << (my_bit_log2(blocks*5/4)+1); while ((length=(uint) blocks*sizeof(SEC_LINK)+ - sizeof(SEC_LINK*)*_my_hash_blocks)+(ulong) blocks*KEYCACHE_BLOCK_SIZE > + sizeof(SEC_LINK*)*_my_hash_blocks)+ + ((ulong) blocks << key_cache_shift) > use_mem) blocks--; - if ((_my_block_mem=my_malloc_lock((ulong) blocks * KEYCACHE_BLOCK_SIZE,MYF(0)))) + if ((_my_block_mem=my_malloc_lock((ulong) blocks << key_cache_shift, + MYF(0)))) { if ((_my_block_root=(SEC_LINK*) my_malloc((uint) length,MYF(0))) != 0) break; @@ -132,21 +133,48 @@ int init_key_cache(ulong use_mem, DBUG_PRINT("exit",("disk_blocks: %d block_root: %lx _my_hash_blocks: %d hash_root: %lx", _my_disk_blocks,_my_block_root,_my_hash_blocks, _my_hash_root)); -#if !defined(HAVE_ALLOCA) && !defined(THREAD) - my_free(extra_mem,MYF(0)); -#endif } bzero((gptr) changed_blocks,sizeof(changed_blocks[0])*CHANGED_BLOCKS_HASH); bzero((gptr) file_blocks,sizeof(file_blocks[0])*CHANGED_BLOCKS_HASH); DBUG_RETURN((int) blocks); + err: - if (extra_mem) /* purecov: inspected */ - my_free(extra_mem,MYF(0)); my_errno=ENOMEM; DBUG_RETURN(0); } /* init_key_cache */ +/* + Resize the key cache + + SYNOPSIS + resize_key_cache() + use_mem Bytes to use for new key cache + + RETURN VALUES + 0 Error + # number of blocks in key cache +*/ + + +int resize_key_cache(ulong use_mem) +{ + int block; + pthread_mutex_lock(&THR_LOCK_keycache); + if (flush_all_key_blocks()) + { + /* TODO: If this happens, we should write a warning in the log file ! */ + pthread_mutex_unlock(&THR_LOCK_keycache); + return 0; + } + end_key_cache(); + /* The following will work even if memory is 0 */ + block=init_key_cache(use_mem); + pthread_mutex_unlock(&THR_LOCK_keycache); + return block; +} + + /* Remove key_cache from memory */ void end_key_cache(void) @@ -162,6 +190,7 @@ void end_key_cache(void) } } key_cache_inited=0; + _my_hash_blocks=_my_blocks_used=0; DBUG_PRINT("status", ("used: %d changed: %d w_requests: %ld writes: %ld r_requests: %ld reads: %ld", _my_blocks_used,_my_blocks_changed,_my_cache_w_requests, @@ -170,17 +199,6 @@ void end_key_cache(void) } /* end_key_cache */ -static uint find_next_bigger_power(uint value) -{ - uint old_value=1; - while (value) - { - old_value=value; - value&= value-1; - } - return (old_value << 1); -} - static inline void link_into_file_blocks(SEC_LINK *next, int file) { reg1 SEC_LINK **ptr= &file_blocks[(uint) file & CHANGED_BLOCKS_MASK]; @@ -243,7 +261,7 @@ static void test_key_cache(const char *where, my_bool lock); /* ** read a key_buffer - ** filepos must point at a even KEYCACHE_BLOCK_SIZE block + ** filepos must point at a even key_cache_block_size block ** if return_buffer is set then the intern buffer is returned if ** it can be used ** Returns adress to where data is read @@ -255,9 +273,12 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, { reg1 SEC_LINK *next; int error=0; + DBUG_ENTER("key_cache_read"); + DBUG_PRINT("enter", ("file %u, filepos %lu, length %u", + (uint) file, (ulong) filepos, length)); #ifndef THREAD - if (block_length > KEYCACHE_BLOCK_SIZE) + if (block_length > key_cache_block_size) return_buffer=0; #endif if (_my_disk_blocks > 0) @@ -265,21 +286,27 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, byte *start=buff; uint read_length; pthread_mutex_lock(&THR_LOCK_keycache); + if (_my_disk_blocks <= 0) /* Resize failed */ + { + pthread_mutex_unlock(&THR_LOCK_keycache); + goto no_key_cache; + } do { _my_cache_r_requests++; - read_length= length > KEYCACHE_BLOCK_SIZE ? KEYCACHE_BLOCK_SIZE : length; + read_length= (length > key_cache_block_size ? key_cache_block_size : + length); if (!(next=find_key_block(file,filepos,&error))) { pthread_mutex_unlock(&THR_LOCK_keycache); - return (byte*) 0; /* Got a fatal error */ + DBUG_RETURN ((byte*) 0); /* Got a fatal error */ } if (error) { /* Didn't find it in cache */ if (my_pread(file,next->buffer,read_length,filepos,MYF(MY_NABP))) { pthread_mutex_unlock(&THR_LOCK_keycache); - return((byte*) 0); + DBUG_RETURN((byte*) 0); } _my_cache_read++; } @@ -287,7 +314,7 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, if (return_buffer) { pthread_mutex_unlock(&THR_LOCK_keycache); - return (next->buffer); + DBUG_RETURN (next->buffer); } #endif if (! (read_length & 511)) @@ -298,19 +325,21 @@ byte *key_cache_read(File file, my_off_t filepos, byte *buff, uint length, filepos+=read_length; } while ((length-= read_length)); pthread_mutex_unlock(&THR_LOCK_keycache); - return(start); + DBUG_RETURN(start); } + +no_key_cache: _my_cache_r_requests++; _my_cache_read++; if (my_pread(file,(byte*) buff,length,filepos,MYF(MY_NABP))) error=1; - return (error ? (byte*) 0 : buff); + DBUG_RETURN(error ? (byte*) 0 : buff); } /* key_cache_read */ /* write a key_buffer */ /* We don't have to use pwrite because of write locking */ - /* buff must point at a even KEYCACHE_BLOCK_SIZE block */ + /* buff must point at a even key_cache_block_size block */ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, uint block_length __attribute__((unused)), @@ -318,12 +347,15 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, { reg1 SEC_LINK *next; int error=0; + DBUG_ENTER("key_cache_write"); + DBUG_PRINT("enter", ("file %u, filepos %lu, length %u", + (uint) file, (ulong) filepos, length)); if (!dont_write) { /* Forced write of buffer */ _my_cache_write++; if (my_pwrite(file,buff,length,filepos,MYF(MY_NABP | MY_WAIT_IF_FULL))) - return(1); + DBUG_RETURN(1); } #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) @@ -333,10 +365,16 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, { /* We have key_cacheing */ uint read_length; pthread_mutex_lock(&THR_LOCK_keycache); + if (_my_disk_blocks <= 0) /* If resize failed */ + { + pthread_mutex_unlock(&THR_LOCK_keycache); + goto no_key_cache; + } + _my_cache_w_requests++; do { - read_length= length > KEYCACHE_BLOCK_SIZE ? KEYCACHE_BLOCK_SIZE : length; + read_length= length > key_cache_block_size ? key_cache_block_size : length; if (!(next=find_key_block(file,filepos,&error))) goto end; /* Fatal error */ if (!dont_write) /* If we wrote buff at start */ @@ -356,8 +394,11 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, } while ((length-= read_length)); error=0; pthread_mutex_unlock(&THR_LOCK_keycache); + goto end; } - else if (dont_write) + +no_key_cache: + if (dont_write) { /* We must write, no cache */ _my_cache_w_requests++; _my_cache_write++; @@ -365,11 +406,12 @@ int key_cache_write(File file, my_off_t filepos, byte *buff, uint length, MYF(MY_NABP | MY_WAIT_IF_FULL))) error=1; } + end: #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) DBUG_EXECUTE("check_keycache",test_key_cache("end of key_cache_write",1);); #endif - return(error); + DBUG_RETURN(error); } /* key_cache_write */ @@ -379,13 +421,16 @@ end: static SEC_LINK *find_key_block(int file, my_off_t filepos, int *error) { reg1 SEC_LINK *next,**start; + DBUG_ENTER("find_key_block"); + DBUG_PRINT("enter", ("file %u, filepos %lu", + (uint) file, (ulong) filepos)); #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) DBUG_EXECUTE("check_keycache2",test_key_cache("start of find_key_block",0);); #endif *error=0; - next= *(start= &_my_hash_root[((ulong) (filepos/KEYCACHE_BLOCK_SIZE)+(ulong) file) & + next= *(start= &_my_hash_root[((ulong) (filepos >> key_cache_shift)+(ulong) file) & (_my_hash_blocks-1)]); while (next && (next->diskpos != filepos || next->file != file)) next= next->next_hash; @@ -411,7 +456,8 @@ static SEC_LINK *find_key_block(int file, my_off_t filepos, int *error) { /* There are unused blocks */ next= &_my_block_root[_my_blocks_used++]; /* Link in hash-chain */ next->buffer=ADD_TO_PTR(_my_block_mem, - (ulong) _my_disk_blocks_used*KEYCACHE_BLOCK_SIZE,byte*); + ((ulong) _my_disk_blocks_used << key_cache_shift), + byte*); /* link first in file_blocks */ next->changed=0; link_into_file_blocks(next,file); @@ -426,7 +472,8 @@ static SEC_LINK *find_key_block(int file, my_off_t filepos, int *error) next= _my_used_first; if (next->changed) { - if (my_pwrite(next->file,next->buffer,KEYCACHE_BLOCK_SIZE,next->diskpos, + if (my_pwrite(next->file,next->buffer,key_cache_block_size, + next->diskpos, MYF(MY_NABP | MY_WAIT_IF_FULL))) { *error=1; @@ -461,7 +508,7 @@ static SEC_LINK *find_key_block(int file, my_off_t filepos, int *error) #if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) DBUG_EXECUTE("check_keycache2",test_key_cache("end of find_key_block",0);); #endif - return next; + DBUG_RETURN(next); } /* find_key_block */ @@ -499,13 +546,15 @@ static int cmp_sec_link(SEC_LINK **a, SEC_LINK **b) ((*a)->diskpos > (*b)->diskpos) ? 1 : 0); } + static int flush_cached_blocks(File file, SEC_LINK **cache, uint count) { uint last_errno=0; qsort((byte*) cache, count, sizeof(*cache), (qsort_cmp) cmp_sec_link); for ( ; count-- ; cache++) { - if (my_pwrite(file,(*cache)->buffer,KEYCACHE_BLOCK_SIZE,(*cache)->diskpos, + if (my_pwrite(file,(*cache)->buffer,key_cache_block_size, + (*cache)->diskpos, MYF(MY_NABP | MY_WAIT_IF_FULL))) { if (!last_errno) @@ -516,25 +565,23 @@ static int flush_cached_blocks(File file, SEC_LINK **cache, uint count) } -int flush_key_blocks(File file, enum flush_type type) +static int flush_key_blocks_int(File file, enum flush_type type) { int error=0,last_errno=0; uint count=0; SEC_LINK *cache_buff[FLUSH_CACHE],**cache,**pos,**end; SEC_LINK *used,*next; - DBUG_ENTER("flush_key_blocks"); + DBUG_ENTER("flush_key_blocks_int"); DBUG_PRINT("enter",("file: %d blocks_used: %d blocks_changed: %d", file,_my_blocks_used,_my_blocks_changed)); - pthread_mutex_lock(&THR_LOCK_keycache); - -#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) - DBUG_EXECUTE("check_keycache",test_key_cache("start of flush_key_blocks",0);); -#endif cache=cache_buff; /* If no key cache */ if (_my_disk_blocks > 0 && (!my_disable_flush_key_blocks || type != FLUSH_KEEP)) { +#if !defined(DBUG_OFF) && defined(EXTRA_DEBUG) + DBUG_EXECUTE("check_keycache",test_key_cache("start of flush_key_blocks",0);); +#endif if (type != FLUSH_IGNORE_CHANGED) { /* Count how many key blocks we have to cache to be able to @@ -606,22 +653,69 @@ int flush_key_blocks(File file, enum flush_type type) free_block(used); } } - } #ifndef DBUG_OFF - DBUG_EXECUTE("check_keycache",test_key_cache("end of flush_key_blocks",0);); + DBUG_EXECUTE("check_keycache",test_key_cache("end of flush_key_blocks",0);); #endif - pthread_mutex_unlock(&THR_LOCK_keycache); + } if (cache != cache_buff) my_free((gptr) cache,MYF(0)); if (last_errno) errno=last_errno; /* Return first error */ DBUG_RETURN(last_errno != 0); -} /* flush_key_blocks */ +} + + +/* + Flush all blocks for a specific file to disk + + SYNOPSIS + flush_all_key_blocks() + file File descriptor + type Type of flush operation + + RETURN VALUES + 0 Ok + 1 Error +*/ + +int flush_key_blocks(File file, enum flush_type type) +{ + int res; + pthread_mutex_lock(&THR_LOCK_keycache); + res=flush_key_blocks_int(file, type); + pthread_mutex_unlock(&THR_LOCK_keycache); + return res; +} + + +/* + Flush all blocks in the key cache to disk + + SYNOPSIS + flush_all_key_blocks() + + NOTE + We must have a lock on THR_LOCK_keycache before calling this function + + RETURN VALUES + 0 Ok + 1 Error +*/ + + +static int flush_all_key_blocks() +{ + int error=0; + while (_my_blocks_changed > 0) + if (flush_key_blocks_int(_my_used_first->file, FLUSH_RELEASE)) + error=1; + return error; +} #ifndef DBUG_OFF - /* Test if disk-cachee is ok */ + /* Test if disk-cache is ok */ static void test_key_cache(const char *where, my_bool lock) { @@ -630,7 +724,14 @@ static void test_key_cache(const char *where, my_bool lock) SEC_LINK *pos,**prev; if (lock) + { pthread_mutex_lock(&THR_LOCK_keycache); + if (_my_disk_blocks <= 0) /* No active key cache */ + { + pthread_mutex_unlock(&THR_LOCK_keycache); + return; + } + } found=error=0; for (i= 0 ; i < _my_hash_blocks ; i++) { @@ -647,7 +748,7 @@ static void test_key_cache(const char *where, my_bool lock) i,(ulong) pos,(ulong) prev,(ulong) pos->prev_hash)); } - if (((pos->diskpos/KEYCACHE_BLOCK_SIZE)+pos->file) % _my_hash_blocks != i) + if (((pos->diskpos >> key_cache_shift)+pos->file) % _my_hash_blocks != i) { DBUG_PRINT("error",("hash: %d pos: %lx : Wrong disk_buffer %ld", i,(ulong) pos,(ulong) pos->diskpos)); diff --git a/mysys/mf_loadpath.c b/mysys/mf_loadpath.c index 641528b8b5d..291ad62e297 100644 --- a/mysys/mf_loadpath.c +++ b/mysys/mf_loadpath.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> @@ -37,8 +36,7 @@ my_string my_load_path(my_string to, const char *path, test_if_hard_path(path)) VOID(strmov(buff,path)); else if ((path[0] == FN_CURLIB && path[1] == FN_LIBCHAR) || - (is_prefix((gptr) path,FN_PARENTDIR) && - path[strlen(FN_PARENTDIR)] == FN_LIBCHAR) || + (is_prefix((gptr) path,FN_PARENTDIR)) || ! own_path_prefix) { if (! my_getwd(buff,(uint) (FN_REFLEN-strlen(path)),MYF(0))) diff --git a/mysys/mf_pack.c b/mysys/mf_pack.c index b442af7e9e5..8fba14f626b 100644 --- a/mysys/mf_pack.c +++ b/mysys/mf_pack.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> @@ -40,6 +39,7 @@ void pack_dirname(my_string to, const char *from) char buff[FN_REFLEN]; DBUG_ENTER("pack_dirname"); + LINT_INIT(buff_length); (void) intern_filename(to,from); /* Change to intern name */ #ifdef FN_DEVCHAR @@ -49,7 +49,6 @@ void pack_dirname(my_string to, const char *from) #endif start=to; - LINT_INIT(buff_length); if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0)))) { buff_length= (uint) strlen(buff); @@ -218,7 +217,7 @@ uint cleanup_dirname(register my_string to, const char *from) to the directory. This will be used if the directory name doesn't exists */ - + my_bool my_use_symdir=0; /* Set this if you want to use symdirs */ @@ -459,8 +458,7 @@ my_string intern_filename(my_string to, const char *from) my_string pos,from_pos,to_pos,end_pos; char buff[FN_REFLEN]; - (void) strmov(buff,from); - convert_dirname(buff); /* change '<>' to '[]' */ + convert_dirname(buff,from,NullS); /* change '<>' to '[]' */ from_pos=buff; if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skipp device part */ { diff --git a/mysys/mf_pack2.c b/mysys/mf_pack2.c deleted file mode 100644 index 1cda7797457..00000000000 --- a/mysys/mf_pack2.c +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ - -#include "mysys_priv.h" -#include <m_string.h> - - /* Pack a filename for output on screen */ - /* Changes long paths to .../ */ - /* Removes pathname and extension */ - /* If not possibly to pack returns '?' in to and returns 1*/ - -int pack_filename(my_string to, const char *name, size_s max_length) - /* to may be name */ - -{ - int i; - char buff[FN_REFLEN]; - - if (strlen(fn_format(to,name,"","",0)) <= max_length) - return 0; - if (strlen(fn_format(to,name,"","",8)) <= max_length) - return 0; - if (strlen(fn_format(buff,name,".../","",1)) <= max_length) - { - VOID(strmov(to,buff)); - return 0; - } - for (i= 0 ; i < 3 ; i++) - { - if (strlen(fn_format(buff,to,"","", i == 0 ? 2 : i == 1 ? 1 : 3 )) - <= max_length) - { - VOID(strmov(to,buff)); - return 0; - } - } - to[0]='?'; to[1]=0; /* Can't pack filename */ - return 1; -} /* pack_filename */ diff --git a/mysys/mf_path.c b/mysys/mf_path.c index 9a88b938e2c..23eadd2acce 100644 --- a/mysys/mf_path.c +++ b/mysys/mf_path.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> diff --git a/mysys/mf_qsort2.c b/mysys/mf_qsort2.c index 7b1ca4e39ab..160bb6df817 100644 --- a/mysys/mf_qsort2.c +++ b/mysys/mf_qsort2.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* qsort that sends one extra argument to the compare subrutine */ diff --git a/mysys/mf_radix.c b/mysys/mf_radix.c index 99aa4d0b073..7ee96b966b9 100644 --- a/mysys/mf_radix.c +++ b/mysys/mf_radix.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Radixsort for pointers to fixed length strings. diff --git a/mysys/mf_same.c b/mysys/mf_same.c index c1a5cae11cb..efd6e7b2ca4 100644 --- a/mysys/mf_same.c +++ b/mysys/mf_same.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Kopierar biblioteksstrukturen och extensionen fr}n ett filnamn */ diff --git a/mysys/mf_sleep.c b/mysys/mf_sleep.c deleted file mode 100644 index f33a904cf5a..00000000000 --- a/mysys/mf_sleep.c +++ /dev/null @@ -1,36 +0,0 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ - -/* Wait a given time (On systems that dont have sleep !!; MSDOS) */ - -#include "mysys_priv.h" -#include <m_string.h> - -#ifdef _MSC_VER - -void sleep(sec) -int sec; -{ - ulong start; - DBUG_ENTER("sleep"); - - start=(ulong) time((time_t*) 0); - while ((ulong) time((time_t*) 0) < start+sec); - DBUG_VOID_RETURN; -} /* sleep */ - -#endif /* MSDOS */ diff --git a/mysys/mf_sort.c b/mysys/mf_sort.c index 754a1deb1a7..0dc6c78a589 100644 --- a/mysys/mf_sort.c +++ b/mysys/mf_sort.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Sort of string pointers in string-order with radix or qsort */ @@ -25,7 +24,7 @@ void my_string_ptr_sort(void *base, uint items, size_s size) #if INT_MAX > 65536L uchar **ptr=0; - if (size <= 20 && items >= 1000 && + if (size <= 20 && items >= 1000 && items < 100000 && (ptr= (uchar**) my_malloc(items*sizeof(char*),MYF(0)))) { radixsort_for_str_ptr((uchar**) base,items,size,ptr); diff --git a/mysys/mf_soundex.c b/mysys/mf_soundex.c index 827385a1466..4f7aa7da601 100644 --- a/mysys/mf_soundex.c +++ b/mysys/mf_soundex.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /**************************************************************** * SOUNDEX ALGORITHM in C * diff --git a/mysys/mf_strip.c b/mysys/mf_strip.c index 682c9c32cb0..ef2aab2c0a3 100644 --- a/mysys/mf_strip.c +++ b/mysys/mf_strip.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* T|mmer en str{ng p{ slut_space */ diff --git a/mysys/mf_tempfile.c b/mysys/mf_tempfile.c index 465311088c1..cca80dcd552 100644 --- a/mysys/mf_tempfile.c +++ b/mysys/mf_tempfile.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> @@ -25,7 +24,7 @@ #endif #ifdef HAVE_TEMPNAM -#if !defined( MSDOS) && !defined(OS2) +#if !defined(MSDOS) && !defined(OS2) && !defined(__NETWARE__) extern char **environ; #endif #endif @@ -90,9 +89,10 @@ File create_temp_file(char *to, const char *dir, const char *prefix, uint pfx_len; File org_file; - pfx_len=(strmov(strnmov(prefix_buff, - prefix ? prefix : "tmp.", - sizeof(prefix_buff)-7),"XXXXXX") - prefix_buff); + pfx_len= (uint) (strmov(strnmov(prefix_buff, + prefix ? prefix : "tmp.", + sizeof(prefix_buff)-7),"XXXXXX") - + prefix_buff); if (!dir && ! (dir =getenv("TMPDIR"))) dir=P_tmpdir; if (strlen(dir)+ pfx_len > FN_REFLEN-2) @@ -100,8 +100,7 @@ File create_temp_file(char *to, const char *dir, const char *prefix, errno=my_errno= ENAMETOOLONG; return 1; } - strmov(to,dir); - strmov(convert_dirname(to),prefix_buff); + strmov(convert_dirname(to,dir,NullS),prefix_buff); org_file=mkstemp(to); file=my_register_filename(org_file, to, FILE_BY_MKSTEMP, EE_CANTCREATEFILE, MyFlags); @@ -123,14 +122,14 @@ File create_temp_file(char *to, const char *dir, const char *prefix, dir=to; } #ifdef OS2 - // changing environ variable doesn't work with VACPP + /* changing environ variable doesn't work with VACPP */ char buffer[256]; sprintf( buffer, "TMP=%s", dir); - // remove ending backslash + /* remove ending backslash */ if (buffer[strlen(buffer)-1] == '\\') buffer[strlen(buffer)-1] = '\0'; putenv( buffer); -#else +#elif !defined(__NETWARE__) old_env= (char**) environ; if (dir) { /* Don't use TMPDIR if dir is given */ @@ -152,7 +151,7 @@ File create_temp_file(char *to, const char *dir, const char *prefix, { DBUG_PRINT("error",("Got error: %d from tempnam",errno)); } -#ifndef OS2 +#if !defined(OS2) && !defined(__NETWARE__) environ=(const char**) old_env; #endif } @@ -178,7 +177,7 @@ File create_temp_file(char *to, const char *dir, const char *prefix, if (end_pos != to && end_pos[-1] != FN_LIBCHAR) *end_pos++=FN_LIBCHAR; end_pos=strmov(end_pos,pfx); - + for (length=0 ; length < 8 && uniq ; length++) { *end_pos++= _dig_vec[(int) (uniq & 31)]; diff --git a/mysys/mf_unixpath.c b/mysys/mf_unixpath.c index 6ae29f99136..9efc3e5d9b8 100644 --- a/mysys/mf_unixpath.c +++ b/mysys/mf_unixpath.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <m_string.h> diff --git a/mysys/mf_util.c b/mysys/mf_util.c index 12af323680e..132c83b4623 100644 --- a/mysys/mf_util.c +++ b/mysys/mf_util.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Utilities with are missing on some systems */ diff --git a/mysys/mf_wcomp.c b/mysys/mf_wcomp.c index 5f1ab9b813e..bdcfb0501d8 100644 --- a/mysys/mf_wcomp.c +++ b/mysys/mf_wcomp.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Funktions for comparing with wild-cards */ diff --git a/mysys/mf_wfile.c b/mysys/mf_wfile.c index 87d1392250a..e9e12c72755 100644 --- a/mysys/mf_wfile.c +++ b/mysys/mf_wfile.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Functions for finding files with wildcards */ diff --git a/mysys/mulalloc.c b/mysys/mulalloc.c index 8a7ee312aa9..6b025bca699 100644 --- a/mysys/mulalloc.c +++ b/mysys/mulalloc.c @@ -1,26 +1,35 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - /* Malloc many pointers at the same time */ - /* format myFlags,ptr,length,ptr,length ... until null ptr */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include <stdarg.h> +/* + Malloc many pointers at the same time + + SYNOPSIS + my_multi_malloc() + myFlags Flags + ... Multiple arguments terminated by null ptr + + ptr, length + ptr, length + NULL +*/ + gptr my_multi_malloc(myf myFlags, ...) { va_list args; diff --git a/mysys/my_aes.c b/mysys/my_aes.c new file mode 100644 index 00000000000..16d326d7d1f --- /dev/null +++ b/mysys/my_aes.c @@ -0,0 +1,228 @@ +/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + + +/* + Implementation of AES Encryption for MySQL + Initial version by Peter Zaitsev June 2002 +*/ + + +#include <my_global.h> +#include <m_string.h> +#include "my_aes.h" + +enum encrypt_dir { AES_ENCRYPT, AES_DECRYPT }; + +#define AES_BLOCK_SIZE 16 /* Block size in bytes */ + +#define AES_BAD_DATA -1 /* If bad data discovered during decoding */ + + +/* The structure for key information */ +typedef struct { + int nr; /* Number of rounds */ + uint32 rk[4*(AES_MAXNR + 1)]; /* key schedule */ +} KEYINSTANCE; + + +/* + This is internal function just keeps joint code of Key generation + + SYNOPSIS + my_aes_create_key() + aes_key Address of Key Instance to be created + direction Direction (are we encoding or decoding) + key Key to use for real key creation + key_length Length of the key + + DESCRIPTION + + RESULT + 0 ok + -1 Error Note: The current impementation never returns this +*/ + +static int my_aes_create_key(KEYINSTANCE *aes_key, + enum encrypt_dir direction, const char *key, + int key_length) +{ + uint8 rkey[AES_KEY_LENGTH/8]; /* The real key to be used for encryption */ + uint8 *rkey_end=rkey+AES_KEY_LENGTH/8; /* Real key boundary */ + uint8 *ptr; /* Start of the real key*/ + const char *sptr; /* Start of the working key */ + const char *key_end=key+key_length; /* Working key boundary*/ + + bzero((char*) rkey,AES_KEY_LENGTH/8); /* Set initial key */ + + for (ptr= rkey, sptr= key; sptr < key_end; ptr++,sptr++) + { + if (ptr == rkey_end) + ptr= rkey; /* Just loop over tmp_key until we used all key */ + *ptr^= (uint8) *sptr; + } +#ifdef AES_USE_KEY_BITS + /* + This block is intended to allow more weak encryption if application + build with libmysqld needs to correspond to export regulations + It should be never used in normal distribution as does not give + any speed improvement. + To get worse security define AES_USE_KEY_BITS to number of bits + you want key to be. It should be divisible by 8 + + WARNING: Changing this value results in changing of enryption for + all key lengths so altering this value will result in impossibility + to decrypt data encrypted with previous value + */ +#define AES_USE_KEY_BYTES (AES_USE_KEY_BITS/8) + /* + To get weaker key we use first AES_USE_KEY_BYTES bytes of created key + and cyclically copy them until we created all required key length + */ + for (ptr= rkey+AES_USE_KEY_BYTES, sptr=rkey ; ptr < rkey_end; + ptr++,sptr++) + { + if (sptr == rkey+AES_USE_KEY_BYTES) + sptr=rkey; + *ptr=*sptr; + } +#endif + if (direction == AES_DECRYPT) + aes_key->nr = rijndaelKeySetupDec(aes_key->rk, rkey, AES_KEY_LENGTH); + else + aes_key->nr = rijndaelKeySetupEnc(aes_key->rk, rkey, AES_KEY_LENGTH); + return 0; +} + + +/* + Crypt buffer with AES encryption algorithm. + + SYNOPSIS + my_aes_encrypt() + source Pointer to data for encryption + source_length Size of encryption data + dest Buffer to place encrypted data (must be large enough) + key Key to be used for encryption + key_length Length of the key. Will handle keys of any length + + RETURN + >= 0 Size of encrypted data + < 0 Error +*/ + +int my_aes_encrypt(const char* source, int source_length, char* dest, + const char* key, int key_length) +{ + KEYINSTANCE aes_key; + uint8 block[AES_BLOCK_SIZE]; /* 128 bit block used for padding */ + int rc; /* result codes */ + int num_blocks; /* number of complete blocks */ + char pad_len; /* pad size for the last block */ + int i; + + if ((rc= my_aes_create_key(&aes_key,AES_ENCRYPT,key,key_length))) + return rc; + + num_blocks = source_length/AES_BLOCK_SIZE; + + for (i = num_blocks; i > 0; i--) /* Encode complete blocks */ + { + rijndaelEncrypt(aes_key.rk, aes_key.nr, (const uint8*) source, + (uint8*) dest); + source+= AES_BLOCK_SIZE; + dest+= AES_BLOCK_SIZE; + } + + /* Encode the rest. We always have incomplete block */ + pad_len = AES_BLOCK_SIZE - (source_length - AES_BLOCK_SIZE*num_blocks); + memcpy(block, source, 16 - pad_len); + bfill(block + AES_BLOCK_SIZE - pad_len, pad_len, pad_len); + rijndaelEncrypt(aes_key.rk, aes_key.nr, block, (uint8*) dest); + return AES_BLOCK_SIZE*(num_blocks + 1); +} + + +/* + DeCrypt buffer with AES encryption algorithm. + + SYNOPSIS + my_aes_decrypt() + source Pointer to data for decryption + source_length Size of encrypted data + dest Buffer to place decrypted data (must be large enough) + key Key to be used for decryption + key_length Length of the key. Will handle keys of any length + + RETURN + >= 0 Size of encrypted data + < 0 Error +*/ + +int my_aes_decrypt(const char *source, int source_length, char *dest, + const char *key, int key_length) +{ + KEYINSTANCE aes_key; + uint8 block[AES_BLOCK_SIZE]; /* 128 bit block used for padding */ + int rc; /* Result codes */ + int num_blocks; /* Number of complete blocks */ + uint pad_len; /* Pad size for the last block */ + int i; + + if ((rc=my_aes_create_key(&aes_key,AES_DECRYPT,key,key_length))) + return rc; + + num_blocks = source_length/AES_BLOCK_SIZE; + + if ((source_length != num_blocks*AES_BLOCK_SIZE) || num_blocks ==0 ) + return AES_BAD_DATA; /* Input size has to be even and at least one block */ + + for (i = num_blocks-1; i > 0; i--) /* Decode all but last blocks */ + { + rijndaelDecrypt(aes_key.rk, aes_key.nr, (const uint8*) source, + (uint8*) dest); + source+= AES_BLOCK_SIZE; + dest+= AES_BLOCK_SIZE; + } + + rijndaelDecrypt(aes_key.rk, aes_key.nr, (const uint8*) source, block); + /* Use last char in the block as size */ + pad_len = (uint) (uchar) block[AES_BLOCK_SIZE-1]; + + if (pad_len > AES_BLOCK_SIZE) + return AES_BAD_DATA; + /* We could also check whole padding but we do not really need this */ + + memcpy(dest, block, AES_BLOCK_SIZE - pad_len); + return AES_BLOCK_SIZE*num_blocks - pad_len; +} + + +/* + Get size of buffer which will be large enough for encrypted data + + SYNOPSIS + my_aes_get_size() + source_length Length of data to be encrypted + + RETURN + Size of buffer required to store encrypted data +*/ + +int my_aes_get_size(int source_length) +{ + return AES_BLOCK_SIZE*(source_length/AES_BLOCK_SIZE)+AES_BLOCK_SIZE; +} diff --git a/mysys/my_alarm.c b/mysys/my_alarm.c index aab01ad77f4..70daf4a9dd0 100644 --- a/mysys/my_alarm.c +++ b/mysys/my_alarm.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Function to set a varible when we got a alarm */ /* Used by my_lock samt functions in m_alarm.h */ diff --git a/mysys/my_alloc.c b/mysys/my_alloc.c index db482454e69..4d3b0604984 100644 --- a/mysys/my_alloc.c +++ b/mysys/my_alloc.c @@ -1,42 +1,47 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Routines to handle mallocing of results which will be freed the same time */ -#include <global.h> +#include <my_global.h> #include <my_sys.h> #include <m_string.h> +#undef EXTRA_DEBUG +#define EXTRA_DEBUG -void init_alloc_root(MEM_ROOT *mem_root, uint block_size, uint pre_alloc_size) +void init_alloc_root(MEM_ROOT *mem_root, uint block_size, + uint pre_alloc_size __attribute__((unused))) { - mem_root->free=mem_root->used=0; - mem_root->min_malloc=32; - mem_root->block_size=block_size-MALLOC_OVERHEAD-sizeof(USED_MEM)-8; - mem_root->error_handler=0; + mem_root->free= mem_root->used= mem_root->pre_alloc= 0; + mem_root->min_malloc= 32; + mem_root->block_size= block_size-MALLOC_OVERHEAD-sizeof(USED_MEM)-8; + mem_root->error_handler= 0; + mem_root->block_num= 4; /* We shift this with >>2 */ + mem_root->first_block_usage= 0; + #if !(defined(HAVE_purify) && defined(EXTRA_DEBUG)) if (pre_alloc_size) { - if ((mem_root->free = mem_root->pre_alloc= + if ((mem_root->free= mem_root->pre_alloc= (USED_MEM*) my_malloc(pre_alloc_size+ ALIGN_SIZE(sizeof(USED_MEM)), MYF(0)))) { - mem_root->free->size=pre_alloc_size+ALIGN_SIZE(sizeof(USED_MEM)); - mem_root->free->left=pre_alloc_size; - mem_root->free->next=0; + mem_root->free->size= pre_alloc_size+ALIGN_SIZE(sizeof(USED_MEM)); + mem_root->free->left= pre_alloc_size; + mem_root->free->next= 0; } } #endif @@ -54,29 +59,37 @@ gptr alloc_root(MEM_ROOT *mem_root,unsigned int Size) (*mem_root->error_handler)(); return((gptr) 0); /* purecov: inspected */ } - next->next=mem_root->used; - mem_root->used=next; + next->next= mem_root->used; + next->size= Size; + mem_root->used= next; return (gptr) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))); #else - uint get_size,max_left; + uint get_size, block_size; gptr point; - reg1 USED_MEM *next; + reg1 USED_MEM *next= 0; reg2 USED_MEM **prev; Size= ALIGN_SIZE(Size); - prev= &mem_root->free; - max_left=0; - for (next= *prev ; next && next->left < Size ; next= next->next) + if ((*(prev= &mem_root->free)) != NULL) { - if (next->left > max_left) - max_left=next->left; - prev= &next->next; + if ((*prev)->left < Size && + mem_root->first_block_usage++ >= ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP && + (*prev)->left < ALLOC_MAX_BLOCK_TO_DROP) + { + next= *prev; + *prev= next->next; /* Remove block from list */ + next->next= mem_root->used; + mem_root->used= next; + mem_root->first_block_usage= 0; + } + for (next= *prev ; next && next->left < Size ; next= next->next) + prev= &next->next; } if (! next) { /* Time to alloc new block */ + block_size= mem_root->block_size * (mem_root->block_num >> 2); get_size= Size+ALIGN_SIZE(sizeof(USED_MEM)); - if (max_left*4 < mem_root->block_size && get_size < mem_root->block_size) - get_size=mem_root->block_size; /* Normal alloc */ + get_size= max(get_size, block_size); if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME)))) { @@ -84,23 +97,55 @@ gptr alloc_root(MEM_ROOT *mem_root,unsigned int Size) (*mem_root->error_handler)(); return((gptr) 0); /* purecov: inspected */ } + mem_root->block_num++; next->next= *prev; next->size= get_size; next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM)); *prev=next; } + point= (gptr) ((char*) next+ (next->size-next->left)); + /*TODO: next part may be unneded due to mem_root->first_block_usage counter*/ if ((next->left-= Size) < mem_root->min_malloc) { /* Full block */ - *prev=next->next; /* Remove block from list */ - next->next=mem_root->used; - mem_root->used=next; + *prev= next->next; /* Remove block from list */ + next->next= mem_root->used; + mem_root->used= next; + mem_root->first_block_usage= 0; } return(point); #endif } - /* deallocate everything used by alloc_root */ +/* Mark all data in blocks free for reusage */ + +static inline void mark_blocks_free(MEM_ROOT* root) +{ + reg1 USED_MEM *next; + reg2 USED_MEM **last; + + /* iterate through (partially) free blocks, mark them free */ + last= &root->free; + for (next= root->free; next; next= *(last= &next->next)) + next->left= next->size - ALIGN_SIZE(sizeof(USED_MEM)); + + /* Combine the free and the used list */ + *last= next=root->used; + + /* now go through the used blocks and mark them free */ + for (; next; next= next->next) + next->left= next->size - ALIGN_SIZE(sizeof(USED_MEM)); + + /* Now everything is set; Indicate that nothing is used anymore */ + root->used= 0; + root->first_block_usage= 0; +} + + +/* + Deallocate everything used by alloc_root or just move + used blocks to free list if called with MY_USED_TO_FREE +*/ void free_root(MEM_ROOT *root, myf MyFlags) { @@ -109,18 +154,23 @@ void free_root(MEM_ROOT *root, myf MyFlags) if (!root) DBUG_VOID_RETURN; /* purecov: inspected */ + if (MyFlags & MY_MARK_BLOCKS_FREE) + { + mark_blocks_free(root); + DBUG_VOID_RETURN; + } if (!(MyFlags & MY_KEEP_PREALLOC)) root->pre_alloc=0; - for ( next=root->used; next ;) + for (next=root->used; next ;) { old=next; next= next->next ; if (old != root->pre_alloc) my_free((gptr) old,MYF(0)); } - for (next= root->free ; next ; ) + for (next=root->free ; next ;) { - old=next; next= next->next ; + old=next; next= next->next; if (old != root->pre_alloc) my_free((gptr) old,MYF(0)); } @@ -131,16 +181,50 @@ void free_root(MEM_ROOT *root, myf MyFlags) root->free->left=root->pre_alloc->size-ALIGN_SIZE(sizeof(USED_MEM)); root->free->next=0; } + root->block_num= 4; + root->first_block_usage= 0; DBUG_VOID_RETURN; } +/* + Find block that contains an object and set the pre_alloc to it +*/ + +void set_prealloc_root(MEM_ROOT *root, char *ptr) +{ + USED_MEM *next; + for (next=root->used; next ; next=next->next) + { + if ((char*) next <= ptr && (char*) next + next->size > ptr) + { + root->pre_alloc=next; + return; + } + } + for (next=root->free ; next ; next=next->next) + { + if ((char*) next <= ptr && (char*) next + next->size > ptr) + { + root->pre_alloc=next; + return; + } + } +} + char *strdup_root(MEM_ROOT *root,const char *str) { - uint len= (uint) strlen(str)+1; + return strmake_root(root, str, strlen(str)); +} + +char *strmake_root(MEM_ROOT *root,const char *str, uint len) +{ char *pos; - if ((pos=alloc_root(root,len))) + if ((pos=alloc_root(root,len+1))) + { memcpy(pos,str,len); + pos[len]=0; + } return pos; } diff --git a/mysys/my_append.c b/mysys/my_append.c index 0d3296fb278..dc5ed084bb3 100644 --- a/mysys/my_append.c +++ b/mysys/my_append.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES /* sys/types is included */ #include "mysys_priv.h" @@ -23,7 +22,7 @@ #include <sys/utime.h> #elif defined(HAVE_UTIME_H) #include <utime.h> -#elif !defined(HPUX) +#elif !defined(HPUX10) struct utimbuf { time_t actime; time_t modtime; diff --git a/mysys/my_bit.c b/mysys/my_bit.c new file mode 100644 index 00000000000..0ff487afe03 --- /dev/null +++ b/mysys/my_bit.c @@ -0,0 +1,31 @@ +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* Some useful bit functions */ + +#include "mysys_priv.h" + +/* + Find smallest X in 2^X >= value + This can be used to divide a number with value by doing a shift instead +*/ + +uint my_bit_log2(ulong value) +{ + uint bit; + for (bit=0 ; value > 1 ; value>>=1, bit++) ; + return bit; +} diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c index a18fcba0b60..8834dda98e1 100644 --- a/mysys/my_bitmap.c +++ b/mysys/my_bitmap.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Handling of uchar arrays as large bitmaps. @@ -26,7 +25,6 @@ #include "mysys_priv.h" #include <my_bitmap.h> #include <assert.h> -#include <string.h> #include <m_string.h> inline void bitmap_lock(MY_BITMAP* map) @@ -50,7 +48,7 @@ my_bool bitmap_init(MY_BITMAP *map, uint bitmap_size, my_bool thread_safe) if (!(map->bitmap=(uchar*) my_malloc((bitmap_size+7)/8, MYF(MY_WME | MY_ZEROFILL)))) return 1; - dbug_assert(bitmap_size != ~(uint) 0); + DBUG_ASSERT(bitmap_size != ~(uint) 0); #ifdef THREAD if ((map->thread_safe = thread_safe)) pthread_mutex_init(&map->mutex, MY_MUTEX_INIT_FAST); @@ -123,6 +121,7 @@ void bitmap_clear_bit(MY_BITMAP *map, uint bitmap_bit) } } + void bitmap_set_all(MY_BITMAP* map) { bitmap_lock(map); diff --git a/mysys/my_chsize.c b/mysys/my_chsize.c index 2c07e8d136f..06e8f159f4a 100644 --- a/mysys/my_chsize.c +++ b/mysys/my_chsize.c @@ -1,28 +1,43 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" #include "m_string.h" - /* Change size of file. Truncate file if shorter, */ - /* else expand with zero. */ +/* + Change size of file. + + SYNOPSIS + my_chsize() + fd File descriptor + new_length New file size + filler If we don't have truncate, fill up all bytes after + new_length with this character + MyFlags Flags + + DESCRIPTION + my_chsize() truncates file if shorter, else fill with the filler character + + RETURN VALUE + 0 Ok + 1 Error +*/ -int my_chsize(File fd, my_off_t newlength, myf MyFlags) +int my_chsize(File fd, my_off_t newlength, int filler, myf MyFlags) { DBUG_ENTER("my_chsize"); DBUG_PRINT("my",("fd: %d length: %lu MyFlags: %d",fd,(ulong) newlength, @@ -68,7 +83,7 @@ int my_chsize(File fd, my_off_t newlength, myf MyFlags) } #endif /* Full file with 0 until it's as big as requested */ - bzero(buff,IO_SIZE); + bfill(buff, IO_SIZE, filler); while (newlength-oldsize > IO_SIZE) { if (my_write(fd,(byte*) buff,IO_SIZE,MYF(MY_NABP))) diff --git a/mysys/my_clock.c b/mysys/my_clock.c index e7d1758fa2e..a192bde056d 100644 --- a/mysys/my_clock.c +++ b/mysys/my_clock.c @@ -1,31 +1,30 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES -#include "global.h" +#include "my_global.h" -#if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(OS2) +#if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(OS2) && !defined(__NETWARE__) #include "mysys_priv.h" #include <sys/times.h> #endif long my_clock(void) { -#if !defined(MSDOS) && !defined(__WIN__) && !defined(OS2) +#if !defined(MSDOS) && !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__) struct tms tmsbuf; VOID(times(&tmsbuf)); return (tmsbuf.tms_utime + tmsbuf.tms_stime); diff --git a/mysys/my_compress.c b/mysys/my_compress.c index f97d28c25ea..dd076311188 100644 --- a/mysys/my_compress.c +++ b/mysys/my_compress.c @@ -1,26 +1,27 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Written by Sinisa Milivojevic <sinisa@coresinc.com> */ +/* Written by Sinisa Milivojevic <sinisa@mysql.com> */ -#include <global.h> +#include <my_global.h> #ifdef HAVE_COMPRESS #include <my_sys.h> +#ifndef SCO #include <m_string.h> +#endif #include <zlib.h> /* @@ -31,24 +32,28 @@ my_bool my_compress(byte *packet, ulong *len, ulong *complen) { + DBUG_ENTER("my_compress"); if (*len < MIN_COMPRESS_LENGTH) + { *complen=0; + DBUG_PRINT("note",("Packet too short: Not compressed")); + } else { byte *compbuf=my_compress_alloc(packet,len,complen); if (!compbuf) - return *complen ? 0 : 1; + DBUG_RETURN(*complen ? 0 : 1); memcpy(packet,compbuf,*len); my_free(compbuf,MYF(MY_WME)); } - return 0; + DBUG_RETURN(0); } byte *my_compress_alloc(const byte *packet, ulong *len, ulong *complen) { byte *compbuf; - *complen = *len * 120 / 100 + 12; - if (!(compbuf = (byte *) my_malloc(*complen,MYF(MY_WME)))) + *complen= *len * 120 / 100 + 12; + if (!(compbuf= (byte *) my_malloc(*complen,MYF(MY_WME)))) return 0; /* Not enough memory */ if (compress((Bytef*) compbuf,(ulong *) complen, (Bytef*) packet, (uLong) *len ) != Z_OK) @@ -58,31 +63,36 @@ byte *my_compress_alloc(const byte *packet, ulong *len, ulong *complen) } if (*complen >= *len) { - *complen=0; - my_free(compbuf,MYF(MY_WME)); + *complen= 0; + my_free(compbuf, MYF(MY_WME)); + DBUG_PRINT("note",("Packet got longer on compression; Not compressed")); return 0; } - swap(ulong,*len,*complen); /* *len is now packet length */ + swap(ulong, *len, *complen); /* *len is now packet length */ return compbuf; } my_bool my_uncompress (byte *packet, ulong *len, ulong *complen) { + DBUG_ENTER("my_uncompress"); if (*complen) /* If compressed */ { - byte *compbuf = (byte *) my_malloc (*complen,MYF(MY_WME)); + byte *compbuf= (byte *) my_malloc(*complen,MYF(MY_WME)); + int error; if (!compbuf) - return 1; /* Not enough memory */ - if (uncompress((Bytef*) compbuf, complen, (Bytef*) packet, *len) != Z_OK) + DBUG_RETURN(1); /* Not enough memory */ + if ((error=uncompress((Bytef*) compbuf, complen, (Bytef*) packet, *len)) + != Z_OK) { /* Probably wrong packet */ - my_free (compbuf,MYF(MY_WME)); - return 1; + DBUG_PRINT("error",("Can't uncompress packet, error: %d",error)); + my_free(compbuf, MYF(MY_WME)); + DBUG_RETURN(1); } - *len = *complen; - memcpy(packet,compbuf,*len); - my_free(compbuf,MYF(MY_WME)); + *len= *complen; + memcpy(packet, compbuf, *len); + my_free(compbuf, MYF(MY_WME)); } - return 0; + DBUG_RETURN(0); } #endif /* HAVE_COMPRESS */ diff --git a/mysys/my_copy.c b/mysys/my_copy.c index 5bc4d1d51fa..84eda781a09 100644 --- a/mysys/my_copy.c +++ b/mysys/my_copy.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES /* sys/types is included */ #include "mysys_priv.h" @@ -23,7 +22,7 @@ #include <sys/utime.h> #elif defined(HAVE_UTIME_H) #include <utime.h> -#elif !defined(HPUX) +#elif !defined(HPUX10) #include <time.h> struct utimbuf { time_t actime; @@ -95,7 +94,7 @@ int my_copy(const char *from, const char *to, myf MyFlags) if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat) DBUG_RETURN(0); /* File copyed but not stat */ VOID(chmod(to, stat_buff.st_mode & 07777)); /* Copy modes */ -#if !defined(MSDOS) && !defined(__WIN__) && !defined(__EMX__) && !defined(OS2) +#if !defined(MSDOS) && !defined(__WIN__) && !defined(__EMX__) && !defined(OS2) && !defined(__NETWARE__) VOID(chown(to, stat_buff.st_uid,stat_buff.st_gid)); /* Copy ownership */ #endif #if !defined(VMS) && !defined(__ZTC__) diff --git a/mysys/my_create.c b/mysys/my_create.c index 5a10b0fd8b5..5fa97a9ca78 100644 --- a/mysys/my_create.c +++ b/mysys/my_create.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES #include "mysys_priv.h" diff --git a/mysys/my_delete.c b/mysys/my_delete.c index 77d5f311418..5670f03da64 100644 --- a/mysys/my_delete.c +++ b/mysys/my_delete.c @@ -1,22 +1,20 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -#include "mysys_priv.h" + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "mysys_priv.h" #include "mysys_err.h" int my_delete(const char *name, myf MyFlags) diff --git a/mysys/my_div.c b/mysys/my_div.c index 24794679376..777ffe403d7 100644 --- a/mysys/my_div.c +++ b/mysys/my_div.c @@ -1,22 +1,29 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" +/* + Get filename of file + + SYNOPSIS + my_filename() + fd File descriptor +*/ + my_string my_filename(File fd) { DBUG_ENTER("my_filename"); diff --git a/mysys/my_dup.c b/mysys/my_dup.c new file mode 100644 index 00000000000..df298780e3e --- /dev/null +++ b/mysys/my_dup.c @@ -0,0 +1,39 @@ +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#define USES_TYPES +#include "mysys_priv.h" +#include "mysys_err.h" +#include <my_dir.h> +#include <errno.h> +#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) +#include <share.h> +#endif + + /* Open a file */ + +File my_dup(File file, myf MyFlags) +{ + File fd; + const char *filename; + DBUG_ENTER("my_dup"); + DBUG_PRINT("my",("file: %d MyFlags: %d", MyFlags)); + fd = dup(file); + filename= (((int) file < MY_NFILE) ? + my_file_info[(int) file].name : "Unknown"); + DBUG_RETURN(my_register_filename(fd, filename, FILE_BY_DUP, + EE_FILENOTFOUND, MyFlags)); +} /* my_open */ diff --git a/mysys/my_error.c b/mysys/my_error.c index 4aa946aa6c3..5ce061212b5 100644 --- a/mysys/my_error.c +++ b/mysys/my_error.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" @@ -26,8 +25,15 @@ const char ** NEAR my_errmsg[MAXMAPS]={0,0,0,0}; char NEAR errbuff[NRERRBUFFS][ERRMSGSIZE]; -/* Error message to user */ -/*VARARGS2*/ +/* + Error message to user + + SYNOPSIS + my_error() + nr Errno + MyFlags Flags + ... variable list +*/ int my_error(int nr,myf MyFlags, ...) { @@ -103,7 +109,16 @@ int my_error(int nr,myf MyFlags, ...) DBUG_RETURN((*error_handler_hook)(nr, ebuff, MyFlags)); } - /* Error as printf */ +/* + Error as printf + + SYNOPSIS + my_printf_error() + error Errno + format Format string + MyFlags Flags + ... variable list +*/ int my_printf_error (uint error, const char *format, myf MyFlags, ...) { @@ -116,7 +131,15 @@ int my_printf_error (uint error, const char *format, myf MyFlags, ...) return (*error_handler_hook)(error, ebuff, MyFlags); } - /* Give message using error_handler_hook */ +/* + Give message using error_handler_hook + + SYNOPSIS + my_message() + error Errno + str Error message + MyFlags Flags +*/ int my_message(uint error, const char *str, register myf MyFlags) { diff --git a/mysys/my_fopen.c b/mysys/my_fopen.c index e1575b0af48..d3b0b90f9c5 100644 --- a/mysys/my_fopen.c +++ b/mysys/my_fopen.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "my_static.h" diff --git a/mysys/my_fstream.c b/mysys/my_fstream.c index d93caadbcdc..94f3aaf3464 100644 --- a/mysys/my_fstream.c +++ b/mysys/my_fstream.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* USE_MY_STREAM isn't set because we can't thrust my_fclose! */ @@ -147,7 +146,8 @@ uint my_fwrite(FILE *stream, const byte *Buffer, uint Count, myf MyFlags) /* Seek to position in file */ /* ARGSUSED */ -my_off_t my_fseek(FILE *stream, my_off_t pos, int whence, myf MyFlags) +my_off_t my_fseek(FILE *stream, my_off_t pos, int whence, + myf MyFlags __attribute__((unused))) { DBUG_ENTER("my_fseek"); DBUG_PRINT("my",("stream: %lx pos: %lu whence: %d MyFlags: %d", @@ -160,7 +160,7 @@ my_off_t my_fseek(FILE *stream, my_off_t pos, int whence, myf MyFlags) /* Tell current position of file */ /* ARGSUSED */ -my_off_t my_ftell(FILE *stream, myf MyFlags) +my_off_t my_ftell(FILE *stream, myf MyFlags __attribute__((unused))) { off_t pos; DBUG_ENTER("my_ftell"); diff --git a/mysys/my_gethostbyname.c b/mysys/my_gethostbyname.c index ffbfc424d0a..5044a505054 100644 --- a/mysys/my_gethostbyname.c +++ b/mysys/my_gethostbyname.c @@ -41,7 +41,7 @@ struct hostent *my_gethostbyname_r(const char *name, int buflen, int *h_errnop) { struct hostent *hp; - dbug_assert((size_t) buflen >= sizeof(*result)); + DBUG_ASSERT((size_t) buflen >= sizeof(*result)); if (gethostbyname_r(name,result, buffer, (size_t) buflen, &hp, h_errnop)) return 0; return hp; @@ -53,7 +53,6 @@ struct hostent *my_gethostbyname_r(const char *name, struct hostent *result, char *buffer, int buflen, int *h_errnop) { - dbug_assert(buflen >= sizeof(struct hostent_data)); if (gethostbyname_r(name,result,(struct hostent_data *) buffer) == -1) { *h_errnop= errno; @@ -71,7 +70,7 @@ struct hostent *my_gethostbyname_r(const char *name, int buflen, int *h_errnop) { struct hostent *hp; - dbug_assert(buflen >= sizeof(struct hostent_data)); + DBUG_ASSERT(buflen >= sizeof(struct hostent_data)); hp= gethostbyname_r(name,result,(struct hostent_data *) buffer); *h_errnop= errno; return hp; diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c new file mode 100644 index 00000000000..759c96462f6 --- /dev/null +++ b/mysys/my_getopt.c @@ -0,0 +1,811 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include <my_global.h> +#include <m_string.h> +#include <stdlib.h> +#include <my_getopt.h> +#include <assert.h> +#include <my_sys.h> +#include <mysys_err.h> + +static int findopt(char *optpat, uint length, + const struct my_option **opt_res, + char **ffname); +my_bool getopt_compare_strings(const char *s, + const char *t, + uint length); +static longlong getopt_ll(char *arg, const struct my_option *optp, int *err); +static ulonglong getopt_ull(char *arg, const struct my_option *optp, + int *err); +static void init_variables(const struct my_option *options); +static int setval(const struct my_option *opts, char *argument, + my_bool set_maximum_value); + +/* + The following three variables belong to same group and the number and + order of their arguments must correspond to each other. +*/ +static const char *special_opt_prefix[]= +{"skip", "disable", "enable", "maximum", "loose", 0}; +static const uint special_opt_prefix_lengths[]= +{ 4, 7, 6, 7, 5, 0}; +enum enum_special_opt +{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE}; + +char *disabled_my_option= (char*) "0"; + +/* + This is a flag that can be set in client programs. 0 means that + my_getopt will not print error messages, but the client should do + it by itself +*/ + +my_bool my_getopt_print_errors= 1; + + +/* + function: handle_options + + Sort options; put options first, until special end of options (--), or + until end of argv. Parse options; check that the given option matches with + one of the options in struct 'my_option', return error in case of ambiguous + or unknown option. Check that option was given an argument if it requires + one. Call function 'get_one_option()' once for each option. +*/ + +int handle_options(int *argc, char ***argv, + const struct my_option *longopts, + my_bool (*get_one_option)(int, + const struct my_option *, + char *)) +{ + uint opt_found, argvpos= 0, length, i; + my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used, + option_is_loose; + char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found; + const struct my_option *optp; + int error; + + LINT_INIT(opt_found); + (*argc)--; /* Skip the program name */ + (*argv)++; /* --- || ---- */ + init_variables(longopts); + + for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++) + { + char *cur_arg= *pos; + if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */ + { + char *argument= 0; + must_be_var= 0; + set_maximum_value= 0; + special_used= 0; + option_is_loose= 0; + + cur_arg++; /* skip '-' */ + if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */ + { /* --set-variable, or -O */ + if (*cur_arg == 'O') + { + must_be_var= 1; + + if (!(*++cur_arg)) /* If not -Ovar=# */ + { + /* the argument must be in next argv */ + if (!*++pos) + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: Option '-O' requires an argument\n", + progname); + return EXIT_ARGUMENT_REQUIRED; + } + cur_arg= *pos; + (*argc)--; + } + } + else if (!getopt_compare_strings(cur_arg, "-set-variable", 13)) + { + must_be_var= 1; + if (cur_arg[13] == '=') + { + cur_arg+= 14; + if (!*cur_arg) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: Option '--set-variable' requires an argument\n", + progname); + return EXIT_ARGUMENT_REQUIRED; + } + } + else if (cur_arg[14]) /* garbage, or another option. break out */ + must_be_var= 0; + else + { + /* the argument must be in next argv */ + if (!*++pos) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: Option '--set-variable' requires an argument\n", + progname); + return EXIT_ARGUMENT_REQUIRED; + } + cur_arg= *pos; + (*argc)--; + } + } + else if (!must_be_var) + { + if (!*++cur_arg) /* skip the double dash */ + { + /* '--' means end of options, look no further */ + end_of_options= 1; + (*argc)--; + continue; + } + } + optend= strcend(cur_arg, '='); + length= optend - cur_arg; + if (*optend == '=') + optend++; + else + optend=0; + + /* + Find first the right option. Return error in case of an ambiguous, + or unknown option + */ + optp= longopts; + if (!(opt_found= findopt(cur_arg, length, &optp, &prev_found))) + { + /* + Didn't find any matching option. Let's see if someone called + option with a special option prefix + */ + if (!must_be_var) + { + if (optend) + must_be_var= 1; /* option is followed by an argument */ + for (i= 0; special_opt_prefix[i]; i++) + { + if (!getopt_compare_strings(special_opt_prefix[i], cur_arg, + special_opt_prefix_lengths[i]) && + cur_arg[special_opt_prefix_lengths[i]] == '-') + { + /* + We were called with a special prefix, we can reuse opt_found + */ + special_used= 1; + cur_arg+= (special_opt_prefix_lengths[i] + 1); + if (i == OPT_LOOSE) + option_is_loose= 1; + if ((opt_found= findopt(cur_arg, length - + (special_opt_prefix_lengths[i] + 1), + &optp, &prev_found))) + { + if (opt_found > 1) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: ambiguous option '--%s-%s' (--%s-%s)\n", + progname, special_opt_prefix[i], cur_arg, + special_opt_prefix[i], prev_found); + return EXIT_AMBIGUOUS_OPTION; + } + switch (i) { + case OPT_SKIP: + case OPT_DISABLE: /* fall through */ + /* + double negation is actually enable again, + for example: --skip-option=0 -> option = TRUE + */ + optend= (optend && *optend == '0' && !(*(optend + 1))) ? + (char*) "1" : disabled_my_option; + break; + case OPT_ENABLE: + optend= (optend && *optend == '0' && !(*(optend + 1))) ? + disabled_my_option : (char*) "1"; + break; + case OPT_MAXIMUM: + set_maximum_value= 1; + must_be_var= 1; + break; + } + break; /* break from the inner loop, main loop continues */ + } + } + } + } + if (!opt_found) + { + if (must_be_var) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: %s: unknown variable '%s'\n", progname, + option_is_loose ? "WARNING" : "ERROR", cur_arg); + if (!option_is_loose) + return EXIT_UNKNOWN_VARIABLE; + } + else + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: %s: unknown option '--%s'\n", progname, + option_is_loose ? "WARNING" : "ERROR", cur_arg); + if (!option_is_loose) + return EXIT_UNKNOWN_OPTION; + } + if (option_is_loose) + { + (*argc)--; + continue; + } + } + } + if (opt_found > 1) + { + if (must_be_var) + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: variable prefix '%s' is not unique\n", + progname, cur_arg); + return EXIT_VAR_PREFIX_NOT_UNIQUE; + } + else + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: ambiguous option '--%s' (%s, %s)\n", + progname, cur_arg, prev_found, optp->name); + return EXIT_AMBIGUOUS_OPTION; + } + } + if (must_be_var && optp->var_type == GET_NO_ARG) + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: option '%s' cannot take an argument\n", + progname, optp->name); + return EXIT_NO_ARGUMENT_ALLOWED; + } + if (optp->arg_type == NO_ARG) + { + if (optend && optp->var_type != GET_BOOL) + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: option '--%s' cannot take an argument\n", + progname, optp->name); + return EXIT_NO_ARGUMENT_ALLOWED; + } + if (optp->var_type == GET_BOOL) + { + /* + Set bool to 1 if no argument or if the user has used + --enable-'option-name'. + *optend was set to '0' if one used --disable-option + */ + *((my_bool*) optp->value)= (my_bool) (!optend || *optend == '1'); + (*argc)--; + get_one_option(optp->id, optp, argument); + continue; + } + argument= optend; + } + else if (optp->arg_type == OPT_ARG && optp->var_type == GET_BOOL) + { + if (optend == disabled_my_option) + *((my_bool*) optp->value)= (my_bool) 0; + else + { + if (!optend) /* No argument -> enable option */ + *((my_bool*) optp->value)= (my_bool) 1; + else /* If argument differs from 0, enable option, else disable */ + *((my_bool*) optp->value)= (my_bool) atoi(optend) != 0; + } + (*argc)--; + continue; + } + else if (optp->arg_type == REQUIRED_ARG && !optend) + { + /* Check if there are more arguments after this one */ + if (!*++pos) + { + if (my_getopt_print_errors) + fprintf(stderr, "%s: option '--%s' requires an argument\n", + progname, optp->name); + return EXIT_ARGUMENT_REQUIRED; + } + argument= *pos; + (*argc)--; + } + else + argument= optend; + } + else /* must be short option */ + { + for (optend= cur_arg; *optend; optend++) + { + opt_found= 0; + for (optp= longopts; optp->id; optp++) + { + if (optp->id == (int) (uchar) *optend) + { + /* Option recognized. Find next what to do with it */ + opt_found= 1; + if (optp->var_type == GET_BOOL && optp->arg_type == NO_ARG) + { + *((my_bool*) optp->value)= (my_bool) 1; + get_one_option(optp->id, optp, argument); + continue; + } + else if (optp->arg_type == REQUIRED_ARG || + optp->arg_type == OPT_ARG) + { + if (*(optend + 1)) + { + /* The rest of the option is option argument */ + argument= optend + 1; + /* This is in effect a jump out of the outer loop */ + optend= (char*) " "; + } + else if (optp->arg_type == REQUIRED_ARG) + { + /* Check if there are more arguments after this one */ + if (!*++pos) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: option '-%c' requires an argument\n", + progname, optp->id); + return EXIT_ARGUMENT_REQUIRED; + } + argument= *pos; + (*argc)--; + /* the other loop will break, because *optend + 1 == 0 */ + } + } + if ((error= setval(optp, argument, set_maximum_value))) + { + fprintf(stderr, + "%s: Error while setting value '%s' to '%s'\n", + progname, argument, optp->name); + return error; + } + get_one_option(optp->id, optp, argument); + break; + } + } + if (!opt_found) + { + if (my_getopt_print_errors) + fprintf(stderr, + "%s: unknown option '-%c'\n", progname, *optend); + return EXIT_UNKNOWN_OPTION; + } + } + (*argc)--; /* option handled (short), decrease argument count */ + continue; + } + if ((error= setval(optp, argument, set_maximum_value))) + { + fprintf(stderr, + "%s: Error while setting value '%s' to '%s'\n", + progname, argument, optp->name); + return error; + } + get_one_option(optp->id, optp, argument); + + (*argc)--; /* option handled (short or long), decrease argument count */ + } + else /* non-option found */ + (*argv)[argvpos++]= cur_arg; + } + /* + Destroy the first, already handled option, so that programs that look + for arguments in 'argv', without checking 'argc', know when to stop. + Items in argv, before the destroyed one, are all non-option -arguments + to the program, yet to be (possibly) handled. + */ + (*argv)[argvpos]= 0; + return 0; +} + +/* + function: setval + + Arguments: opts, argument + Will set the option value to given value +*/ + +static int setval(const struct my_option *opts, char *argument, + my_bool set_maximum_value) +{ + int err= 0; + + if (opts->value && argument) + { + gptr *result_pos= ((set_maximum_value) ? + opts->u_max_value : opts->value); + + if (!result_pos) + return EXIT_NO_PTR_TO_VARIABLE; + + switch (opts->var_type) { + case GET_INT: + case GET_UINT: /* fall through */ + *((int*) result_pos)= (int) getopt_ll(argument, opts, &err); + break; + case GET_LONG: + case GET_ULONG: /* fall through */ + *((long*) result_pos)= (long) getopt_ll(argument, opts, &err); + break; + case GET_LL: + *((longlong*) result_pos)= getopt_ll(argument, opts, &err); + break; + case GET_ULL: + *((ulonglong*) result_pos)= getopt_ull(argument, opts, &err); + break; + case GET_STR: + *((char**) result_pos)= argument; + break; + case GET_STR_ALLOC: + if ((*((char**) result_pos))) + my_free((*(char**) result_pos), + MYF(MY_WME | MY_FAE)); + if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME)))) + return EXIT_OUT_OF_MEMORY; + break; + default: /* dummy default to avoid compiler warnings */ + break; + } + if (err) + return EXIT_UNKNOWN_SUFFIX; + } + return 0; +} + +/* + function: findopt + + Arguments: opt_pattern, length of opt_pattern, opt_struct, first found + name (ffname) + + Go through all options in the my_option struct. Return number + of options found that match the pattern and in the argument + list the option found, if any. In case of ambiguous option, store + the name in ffname argument +*/ + +static int findopt(char *optpat, uint length, + const struct my_option **opt_res, + char **ffname) +{ + int count; + struct my_option *opt= (struct my_option *) *opt_res; + + for (count= 0; opt->name; opt++) + { + if (!getopt_compare_strings(opt->name, optpat, length)) /* match found */ + { + (*opt_res)= opt; + if (!count) + *ffname= (char *) opt->name; /* We only need to know one prev */ + if (!opt->name[length]) /* Exact match */ + return 1; + count++; + } + } + return count; +} + + +/* + function: compare_strings + + Works like strncmp, other than 1.) considers '-' and '_' the same. + 2.) Returns -1 if strings differ, 0 if they are equal +*/ + +my_bool getopt_compare_strings(register const char *s, register const char *t, + uint length) +{ + char const *end= s + length; + for (;s != end ; s++, t++) + { + if ((*s != '-' ? *s : '_') != (*t != '-' ? *t : '_')) + return 1; + } + return 0; +} + +/* + function: eval_num_suffix + + Transforms a number with a suffix to real number. Suffix can + be k|K for kilo, m|M for mega or g|G for giga. +*/ + +static longlong eval_num_suffix (char *argument, int *error, char *option_name) +{ + char *endchar; + longlong num; + + *error= 0; + num= strtoll(argument, &endchar, 10); + if (*endchar == 'k' || *endchar == 'K') + num*= 1024L; + else if (*endchar == 'm' || *endchar == 'M') + num*= 1024L * 1024L; + else if (*endchar == 'g' || *endchar == 'G') + num*= 1024L * 1024L * 1024L; + else if (*endchar) + { + fprintf(stderr, + "Unknown suffix '%c' used for variable '%s' (value '%s')\n", + *endchar, option_name, argument); + *error= 1; + return 0; + } + return num; +} + +/* + function: getopt_ll + + Evaluates and returns the value that user gave as an argument + to a variable. Recognizes (case insensitive) K as KILO, M as MEGA + and G as GIGA bytes. Some values must be in certain blocks, as + defined in the given my_option struct, this function will check + that those values are honored. + In case of an error, set error value in *err. +*/ + +static longlong getopt_ll(char *arg, const struct my_option *optp, int *err) +{ + longlong num; + + num= eval_num_suffix(arg, err, (char*) optp->name); + if (num < (longlong) optp->min_value) + num= (longlong) optp->min_value; + else if (num > 0 && (ulonglong) num > (ulonglong) (ulong) optp->max_value + && optp->max_value) /* if max value is not set -> no upper limit */ + num= (longlong) (ulong) optp->max_value; + num= ((num - (longlong) optp->sub_size) / (optp->block_size ? + (ulonglong) optp->block_size : + 1L)); + return (longlong) (num * (optp->block_size ? (ulonglong) optp->block_size : + 1L)); +} + +/* + function: getopt_ull + + This is the same as getopt_ll, but is meant for unsigned long long + values. +*/ + +static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err) +{ + ulonglong num; + + num= eval_num_suffix(arg, err, (char*) optp->name); + return getopt_ull_limit_value(num, optp); +} + + +ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp) +{ + if ((ulonglong) num > (ulonglong) (ulong) optp->max_value && + optp->max_value) /* if max value is not set -> no upper limit */ + num= (ulonglong) (ulong) optp->max_value; + if (optp->block_size > 1) + { + num/= (ulonglong) optp->block_size; + num*= (ulonglong) optp->block_size; + } + if (num < (ulonglong) optp->min_value) + num= (ulonglong) optp->min_value; + return num; +} + +/* + function: init_variables + + initialize all variables to their default values +*/ + +static void init_variables(const struct my_option *options) +{ + for (; options->name; options++) + { + if (options->value) + { + switch (options->var_type) { + case GET_BOOL: + if (options->u_max_value) + *((my_bool*) options->u_max_value)= (my_bool) options->max_value; + *((my_bool*) options->value)= (my_bool) options->def_value; + break; + case GET_INT: + if (options->u_max_value) + *((int*) options->u_max_value)= (int) options->max_value; + *((int*) options->value)= (int) options->def_value; + break; + case GET_UINT: + if (options->u_max_value) + *((uint*) options->u_max_value)= (uint) options->max_value; + *((uint*) options->value)= (uint) options->def_value; + break; + case GET_LONG: + if (options->u_max_value) + *((long*) options->u_max_value)= (long) options->max_value; + *((long*) options->value)= (long) options->def_value; + break; + case GET_ULONG: + if (options->u_max_value) + *((ulong*) options->u_max_value)= (ulong) options->max_value; + *((ulong*) options->value)= (ulong) options->def_value; + break; + case GET_LL: + if (options->u_max_value) + *((longlong*) options->u_max_value)= (longlong) options->max_value; + *((longlong*) options->value)= (longlong) options->def_value; + break; + case GET_ULL: + if (options->u_max_value) + *((ulonglong*) options->u_max_value)= (ulonglong) options->max_value; + *((ulonglong*) options->value)= (ulonglong) options->def_value; + break; + default: /* dummy default to avoid compiler warnings */ + break; + } + } + } +} + + +/* + function: my_print_options + + Print help for all options and variables. +*/ + +void my_print_help(const struct my_option *options) +{ + uint col, name_space= 22, comment_space= 57; + const char *line_end; + const struct my_option *optp; + + for (optp= options; optp->id; optp++) + { + if (optp->id < 256) + { + printf(" -%c%s", optp->id, strlen(optp->name) ? ", " : " "); + col= 6; + } + else + { + printf(" "); + col= 2; + } + if (strlen(optp->name)) + { + printf("--%s", optp->name); + col+= 2 + strlen(optp->name); + if (optp->var_type == GET_STR || optp->var_type == GET_STR_ALLOC) + { + printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "", + optp->arg_type == OPT_ARG ? "]" : ""); + col+= (optp->arg_type == OPT_ARG) ? 8 : 6; + } + else if (optp->var_type == GET_NO_ARG || optp->var_type == GET_BOOL) + { + putchar(' '); + col++; + } + else + { + printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "", + optp->arg_type == OPT_ARG ? "]" : ""); + col+= (optp->arg_type == OPT_ARG) ? 5 : 3; + } + if (col > name_space && optp->comment && *optp->comment) + { + putchar('\n'); + col= 0; + } + } + for (; col < name_space; col++) + putchar(' '); + if (optp->comment && *optp->comment) + { + const char *comment= optp->comment, *end= strend(comment); + + while ((uint) (end - comment) > comment_space) + { + for (line_end= comment + comment_space; *line_end != ' '; line_end--); + for (; comment != line_end; comment++) + putchar(*comment); + comment++; /* skip the space, as a newline will take it's place now */ + putchar('\n'); + for (col= 0; col < name_space; col++) + putchar(' '); + } + printf("%s", comment); + } + putchar('\n'); + } +} + + +/* + function: my_print_options + + Print variables. +*/ + +void my_print_variables(const struct my_option *options) +{ + uint name_space= 34, length; + char buff[255]; + const struct my_option *optp; + + printf("\nVariables (--variable-name=value)\n"); + printf("and boolean options {FALSE|TRUE} Value (after reading options)\n"); + printf("--------------------------------- -----------------------------\n"); + for (optp= options; optp->id; optp++) + { + if (optp->value) + { + printf("%s", optp->name); + length= strlen(optp->name); + for (; length < name_space; length++) + putchar(' '); + switch (optp->var_type) { + case GET_STR: + case GET_STR_ALLOC: /* fall through */ + printf("%s\n", *((char**) optp->value) ? *((char**) optp->value) : + "(No default value)"); + break; + case GET_BOOL: + printf("%s\n", *((my_bool*) optp->value) ? "TRUE" : "FALSE"); + break; + case GET_INT: + printf("%d\n", *((int*) optp->value)); + break; + case GET_UINT: + printf("%d\n", *((uint*) optp->value)); + break; + case GET_LONG: + printf("%lu\n", *((long*) optp->value)); + break; + case GET_ULONG: + printf("%lu\n", *((ulong*) optp->value)); + break; + case GET_LL: + printf("%s\n", llstr(*((longlong*) optp->value), buff)); + break; + case GET_ULL: + longlong2str(*((ulonglong*) optp->value), buff, 10); + printf("%s\n", buff); + break; + default: /* dummy default to avoid compiler warnings */ + break; + } + } + } +} diff --git a/mysys/my_getwd.c b/mysys/my_getwd.c index 6bdea813fe4..63ab17b4c51 100644 --- a/mysys/my_getwd.c +++ b/mysys/my_getwd.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* my_setwd() and my_getwd() works with intern_filenames !! */ @@ -33,7 +32,7 @@ #endif #ifdef __EMX__ -// chdir2 support also drive change +/* chdir2 support also drive change */ #define chdir _chdir2 #endif diff --git a/mysys/my_init.c b/mysys/my_init.c index c1b2260c121..61d9f4a4b2b 100644 --- a/mysys/my_init.c +++ b/mysys/my_init.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000-2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "my_static.h" @@ -42,9 +41,14 @@ static my_bool win32_init_tcp_ip(); #else #define my_win_init() #endif -static my_bool my_init_done=0; +#ifdef __NETWARE__ +static void netware_init(); +#else +#define netware_init() +#endif +my_bool my_init_done=0; static ulong atoi_octal(const char *str) { @@ -66,12 +70,16 @@ void my_init(void) if (my_init_done) return; my_init_done=1; +#if defined(THREAD) && defined(SAFE_MUTEX) + safe_mutex_global_init(); /* Must be called early */ +#endif + netware_init(); #ifdef THREAD #if defined(HAVE_PTHREAD_INIT) pthread_init(); /* Must be called before DBUG_ENTER */ #endif my_thread_global_init(); -#if !defined( __WIN__) && !defined(OS2) +#if !defined( __WIN__) && !defined(OS2) && !defined(__NETWARE__) sigfillset(&my_signals); /* signals blocked by mf_brkhant */ #endif #endif /* THREAD */ @@ -145,7 +153,7 @@ Voluntary context switches %ld, Involuntary context switches %ld\n", rus.ru_msgsnd, rus.ru_msgrcv, rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw); #endif -#if defined(MSDOS) && !defined(__WIN__) +#if ( defined(MSDOS) || defined(__NETWARE__) ) && !defined(__WIN__) fprintf(info_file,"\nRun time: %.1f\n",(double) clock()/CLOCKS_PER_SEC); #endif #if defined(SAFEMALLOC) @@ -162,20 +170,27 @@ Voluntary context switches %ld, Involuntary context switches %ld\n", #endif } #ifdef THREAD - pthread_mutex_destroy(&THR_LOCK_keycache); - pthread_mutex_destroy(&THR_LOCK_malloc); - pthread_mutex_destroy(&THR_LOCK_open); DBUG_POP(); /* Must be done before my_thread_end */ + my_once_free(); my_thread_end(); my_thread_global_end(); -#endif +#if defined(SAFE_MUTEX) + /* + Check on destroying of mutexes. A few may be left that will get cleaned + up by C++ destructors + */ + safe_mutex_end(infoflag & MY_GIVE_INFO ? stderr : (FILE *) 0); +#endif /* defined(SAFE_MUTEX) */ +#endif /* THREAD */ + #ifdef __WIN__ - if (have_tcpip); - WSACleanup( ); + if (have_tcpip) + WSACleanup(); #endif /* __WIN__ */ - my_init_done=0; + my_init_done=0; } /* my_end */ + #ifdef __WIN__ /* @@ -264,10 +279,10 @@ static void my_win_init(void) /*------------------------------------------------------------------ -** Name: CheckForTcpip| Desc: checks if tcpip has been installed on system -** According to Microsoft Developers documentation the first registry -** entry should be enough to check if TCP/IP is installed, but as expected -** this doesn't work on all Win32 machines :( + Name: CheckForTcpip| Desc: checks if tcpip has been installed on system + According to Microsoft Developers documentation the first registry + entry should be enough to check if TCP/IP is installed, but as expected + this doesn't work on all Win32 machines :( ------------------------------------------------------------------*/ #define TCPIPKEY "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters" @@ -293,6 +308,7 @@ static my_bool win32_have_tcpip(void) return (TRUE); } + static my_bool win32_init_tcp_ip() { if (win32_have_tcpip()) @@ -324,4 +340,64 @@ static my_bool win32_init_tcp_ip() } return(0); } -#endif +#endif /* __WIN__ */ + + +#ifdef __NETWARE__ +/**************************************************************************** + Do basic initialisation for netware needed by most programs +****************************************************************************/ + +static void netware_init() +{ + char cwd[PATH_MAX], *name; + + /* init only if we are not a client library */ + if (my_progname) + { +#if SUPPORTED_BY_LIBC /* Removed until supported in Libc */ + struct termios tp; + /* Disable control characters */ + tcgetattr(STDIN_FILENO, &tp); + tp.c_cc[VINTR] = _POSIX_VDISABLE; + tp.c_cc[VEOF] = _POSIX_VDISABLE; + tp.c_cc[VSUSP] = _POSIX_VDISABLE; + tcsetattr(STDIN_FILENO, TCSANOW, &tp); +#endif /* SUPPORTED_BY_LIBC */ + + /* With stdout redirection */ + if (!isatty(STDOUT_FILENO)) + { + setscreenmode(SCR_AUTOCLOSE_ON_EXIT); /* auto close the screen */ + } + else + { + setscreenmode(SCR_NO_MODE); /* keep the screen up */ + } + + /* Parse program name and change to base format */ + name= my_progname; + for (; *name; name++) + { + if (*name == '\\') + { + *name = '/'; + } + else + { + *name = tolower(*name); + } + } + /* + Set the current working directory to the base directory of the file + name (assuming the binary is in 'base-file-name/bin/' + */ + strmov(cwd, my_progname); + if ((name= strindex(cwd, "/bin/")) != NULL) + { + *name= 0; + chdir(cwd); + } + } +} +#endif /* __NETWARE__ */ diff --git a/mysys/my_lib.c b/mysys/my_lib.c index f9774d8a4aa..a06120894c5 100644 --- a/mysys/my_lib.c +++ b/mysys/my_lib.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* TODO: check for overun of memory for names. */ /* Convert MSDOS-TIME to standar time_t */ @@ -112,6 +111,10 @@ MY_DIR *my_dir(const char *path, myf MyFlags) dirp = opendir(directory_file_name(tmp_path,(my_string) path)); size = STARTSIZE; +#if defined(__amiga__) + if ((dirp->dd_fd) < 0) /* Directory doesn't exists */ + goto error; +#endif if (dirp == NULL || ! (buffer = (char *) my_malloc(size, MyFlags))) goto error; @@ -183,7 +186,7 @@ MY_DIR *my_dir(const char *path, myf MyFlags) my_errno=errno; if (dirp) (void) closedir(dirp); - if (MyFlags & (MY_FAE+MY_WME)) + if (MyFlags & (MY_FAE | MY_WME)) my_error(EE_DIR,MYF(ME_BELL+ME_WAITTANG),path,my_errno); DBUG_RETURN((MY_DIR *) NULL); } /* my_dir */ @@ -580,13 +583,15 @@ error: ** Note that MY_STAT is assumed to be same as struct stat ****************************************************************************/ -int my_fstat(int Filedes, MY_STAT *stat_area, myf MyFlags ) +int my_fstat(int Filedes, MY_STAT *stat_area, + myf MyFlags __attribute__((unused))) { DBUG_ENTER("my_fstat"); DBUG_PRINT("my",("fd: %d MyFlags: %d",Filedes,MyFlags)); DBUG_RETURN(fstat(Filedes, (struct stat *) stat_area)); } + MY_STAT *my_stat(const char *path, MY_STAT *stat_area, myf my_flags) { int m_used; @@ -611,4 +616,3 @@ error: } DBUG_RETURN((MY_STAT *) NULL); } /* my_stat */ - diff --git a/mysys/my_lock.c b/mysys/my_lock.c index c002f447f59..5058c301adb 100644 --- a/mysys/my_lock.c +++ b/mysys/my_lock.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000-2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" @@ -32,6 +31,9 @@ #define INCL_NOPMAPI #include <os2emx.h> #endif +#ifdef __NETWARE__ +#include <nks/fsio.h> +#endif /* Lock a part of a file */ @@ -42,6 +44,9 @@ int my_lock(File fd, int locktype, my_off_t start, my_off_t length, int value; ALARM_VARIABLES; #endif +#ifdef __NETWARE__ + int nxErrno; +#endif DBUG_ENTER("my_lock"); DBUG_PRINT("my",("Fd: %d Op: %d start: %ld Length: %ld MyFlags: %d", fd,locktype,(long) start,(long) length,MyFlags)); @@ -51,7 +56,47 @@ int my_lock(File fd, int locktype, my_off_t start, my_off_t length, if (my_disable_locking) DBUG_RETURN(0); -#if defined(__EMX__) || defined(OS2) +#if defined(__NETWARE__) + { + NXSOffset_t nxLength = length; + unsigned long nxLockFlags = 0; + + if (length == F_TO_EOF) + { + /* EOF is interpreted as a very large length. */ + nxLength = 0x7FFFFFFFFFFFFFFF; + } + + if (locktype == F_UNLCK) + { + /* The lock flags are currently ignored by NKS. */ + if (!(nxErrno= NXFileRangeUnlock(fd, 0L, start, nxLength))) + DBUG_RETURN(0); + } + else + { + if (locktype == F_RDLCK) + { + /* A read lock is mapped to a shared lock. */ + nxLockFlags = NX_RANGE_LOCK_SHARED; + } + else + { + /* A write lock is mapped to an exclusive lock. */ + nxLockFlags = NX_RANGE_LOCK_EXCL; + } + + if (MyFlags & MY_DONT_WAIT) + { + /* Don't block on the lock. */ + nxLockFlags |= NX_RANGE_LOCK_TRYLOCK; + } + + if (!(nxErrno= NXFileRangeLock(fd, nxLockFlags, start, nxLength))) + DBUG_RETURN(0); + } + } +#elif defined(__EMX__) || defined(OS2) if (!_lock64( fd, locktype, start, length, MyFlags)) DBUG_RETURN(0); @@ -104,8 +149,12 @@ int my_lock(File fd, int locktype, my_off_t start, my_off_t length, #endif /* HAVE_FCNTL */ #endif /* HAVE_LOCKING */ +#ifdef __NETWARE__ + my_errno = nxErrno; +#else /* We got an error. We don't want EACCES errors */ my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1; +#endif if (MyFlags & MY_WME) { if (locktype == F_UNLCK) diff --git a/mysys/my_lockmem.c b/mysys/my_lockmem.c index 9c77c885797..6712c387a71 100644 --- a/mysys/my_lockmem.c +++ b/mysys/my_lockmem.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Alloc a block of locked memory */ diff --git a/mysys/my_lread.c b/mysys/my_lread.c index 94ed258151e..601d772b844 100644 --- a/mysys/my_lread.c +++ b/mysys/my_lread.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/my_lwrite.c b/mysys/my_lwrite.c index 734916173ce..e1a3decd053 100644 --- a/mysys/my_lwrite.c +++ b/mysys/my_lwrite.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/my_malloc.c b/mysys/my_malloc.c index c2df22ec7ec..b273363aaf1 100644 --- a/mysys/my_malloc.c +++ b/mysys/my_malloc.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ #undef SAFEMALLOC @@ -74,7 +73,7 @@ gptr my_memdup(const byte *from, uint length, myf MyFlags) } -my_string my_strdup(const char *from, myf MyFlags) +char *my_strdup(const char *from, myf MyFlags) { gptr ptr; uint length=(uint) strlen(from)+1; @@ -82,3 +81,15 @@ my_string my_strdup(const char *from, myf MyFlags) memcpy((byte*) ptr, (byte*) from,(size_t) length); return((my_string) ptr); } + + +char *my_strdup_with_length(const byte *from, uint length, myf MyFlags) +{ + gptr ptr; + if ((ptr=my_malloc(length+1,MyFlags)) != 0) + { + memcpy((byte*) ptr, (byte*) from,(size_t) length); + ((char*) ptr)[length]=0; + } + return((char*) ptr); +} diff --git a/mysys/my_messnc.c b/mysys/my_messnc.c index 0dd3be5ba98..1f9df3c7c2c 100644 --- a/mysys/my_messnc.c +++ b/mysys/my_messnc.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" @@ -24,7 +23,11 @@ int my_message_no_curses(uint error __attribute__((unused)), DBUG_PRINT("enter",("message: %s",str)); (void) fflush(stdout); if (MyFlags & ME_BELL) +#ifdef __NETWARE__ + ringbell(); /* Bell */ +#else (void) fputc('\007',stderr); /* Bell */ +#endif /* __NETWARE__ */ if (my_progname) { (void)fputs(my_progname,stderr); (void)fputs(": ",stderr); diff --git a/mysys/my_mkdir.c b/mysys/my_mkdir.c index 3685312132c..ba1f4c1f2d8 100644 --- a/mysys/my_mkdir.c +++ b/mysys/my_mkdir.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/my_net.c b/mysys/my_net.c index 575c843f297..be92adae353 100644 --- a/mysys/my_net.c +++ b/mysys/my_net.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* thread safe version of some common functions */ diff --git a/mysys/my_netware.c b/mysys/my_netware.c new file mode 100644 index 00000000000..e41dbd0a029 --- /dev/null +++ b/mysys/my_netware.c @@ -0,0 +1,151 @@ +/* Copyright (C) 2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + Function specific to netware +*/ + +#include <mysys_priv.h> +#ifdef __NETWARE__ + #include <string.h> + #include <library.h> + +/* + PMUserLicenseRequest is an API exported by the polimgr.nlm + (loaded by the NetWare OS when it comes up) for use by other + NLM-based NetWare products/services. + PMUserLicenseRequest provides a couple of functions: + 1) it will optionally request a User license or ensure that + one already exists for the specified User in userInfo + 2) it utilizes the NetWare usage metering service to + record usage information about your product/service. +*/ + +long PMMeteredUsageRequest +( + /* + NDS distinguished name or IP address or ??. asciiz string, e.g. + ".CN=Admin.O=this.T=MYTREE." + */ + char *userInfo, + long infoType, /* see defined values */ + /* + string used to identify the calling service, used to index the + metered info e.g. "iPrint" + */ + char *serviceID, + char tranAddrType, /* type of address that follows */ + char *tranAddr, /* ptr to a 10-byte array */ + long flags, /* see defined values */ + /* NLS error code, if any. NULL input is okay */ + long *licRequestErrCode, + /* meter service error code, if any. NULL input is okay */ + long *storeMeterInfoErrCode, + /* + error code from NLSMeter if + storeMeterInfoErrCode == PM_LICREQ_NLSMETERERROR. + NULL input is okay + */ + long *NLSMeterErrCode +); + +typedef long(*PMUR)(const char*, long, const char*, char, + const char*, long, long*, long*, long*); + +/* infoType */ +/* indicates that the info in the userInfo param is an NDS user */ +#define PM_USERINFO_TYPE_NDS 1 +/* indicates that the info in the userInfo param is NOT an NDS user */ +#define PM_USERINFO_TYPE_ADDRESS 2 + +/* Flags */ + +/* + Tells the service that it should not check to see if the NDS user + contained in the userInfo param has a NetWare User License - just + record metering information; this is ignored if infoType != + PM_USERINFO_TYPE_NDS +*/ + +#define PM_FLAGS_METER_ONLY 0x0000001 + +/* + Indicates that the values in the userInfo and serviceID parameters + are unicode strings, so that the metering service bypasses + converting these to unicode (again) +*/ +#define PM_LICREQ_ALREADY_UNICODE 0x0000002 +/* + Useful only if infoType is PM_USERINFO_TYPE_NDS - indicates a "no + stop" policy of the calling service +*/ +#define PM_LICREQ_ALWAYS_METER 0x0000004 + + +/* + net Address Types - system-defined types of net addresses that can + be used in the tranAddrType field +*/ + +#define NLS_TRAN_TYPE_IPX 0x00000001 /* An IPX address */ +#define NLS_TRAN_TYPE_IP 0x00000008 /* An IP address */ +#define NLS_ADDR_TYPE_MAC 0x000000F1 /* a MAC address */ + +/* + Net Address Sizes - lengths that correspond to the tranAddrType + field (just fyi) +*/ +#define NLS_IPX_ADDR_SIZE 10 /* the size of an IPX address */ +#define NLS_IP_ADDR_SIZE 4 /* the size of an IP address */ +#define NLS_MAC_ADDR_SIZE 6 /* the size of a MAC address */ + + +void netware_reg_user(const char *ip, const char *user, + const char *application) +{ + PMUR usage_request; + long licRequestErrCode = 0; + long storeMeterInfoErrCode = 0; + long nlsMeterErrCode = 0; + + /* import the symbol */ + usage_request= ((PMUR)ImportPublicObject(getnlmhandle(), + "PMMeteredUsageRequest")); + if (usage_request != NULL) + { + unsigned long iaddr; + char addr[NLS_IPX_ADDR_SIZE]; + + /* create address */ + iaddr = htonl(inet_addr(ip)); + bzero(addr, NLS_IPX_ADDR_SIZE); + memcpy(addr, &iaddr, NLS_IP_ADDR_SIZE); + + /* call to NLS */ + usage_request(user, + PM_USERINFO_TYPE_ADDRESS, + application, + NLS_TRAN_TYPE_IP, + addr, + PM_FLAGS_METER_ONLY, + &licRequestErrCode, + &storeMeterInfoErrCode, + &nlsMeterErrCode); + /* release symbol */ + UnImportPublicObject(getnlmhandle(), "PMMeteredUsageRequest"); + } +} +#endif /* __NETWARE__ */ diff --git a/mysys/my_once.c b/mysys/my_once.c index 0b8b5addc16..1250ce24994 100644 --- a/mysys/my_once.c +++ b/mysys/my_once.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Not MT-SAFE */ @@ -25,8 +24,17 @@ #include "my_static.h" #include "mysys_err.h" - /* alloc for things we don't nead to free */ - /* No DBUG_ENTER... here to get smaller dbug-startup */ +/* + Alloc for things we don't nead to free + + SYNOPSIS + my_once_alloc() + Size + MyFlags + + NOTES + No DBUG_ENTER... here to get smaller dbug-startup +*/ gptr my_once_alloc(unsigned int Size, myf MyFlags) { @@ -70,7 +78,12 @@ gptr my_once_alloc(unsigned int Size, myf MyFlags) } /* my_once_alloc */ - /* deallocate everything used by my_once_alloc */ +/* + Deallocate everything used by my_once_alloc + + SYNOPSIS + my_once_free() +*/ void my_once_free(void) { diff --git a/mysys/my_open.c b/mysys/my_open.c index 748113528a1..97f21724e1c 100644 --- a/mysys/my_open.c +++ b/mysys/my_open.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES #include "mysys_priv.h" @@ -24,7 +23,18 @@ #include <share.h> #endif - /* Open a file */ +/* + Open a file + + SYNOPSIS + my_open() + FileName Fully qualified file name + Flags Read | write + MyFlags Special flags + + RETURN VALUE + File descriptor +*/ File my_open(const char *FileName, int Flags, myf MyFlags) /* Path-name of file */ @@ -52,7 +62,15 @@ File my_open(const char *FileName, int Flags, myf MyFlags) } /* my_open */ - /* Close a file */ +/* + Close a file + + SYNOPSIS + my_close() + fd File sescriptor + myf Special Flags + +*/ int my_close(File fd, myf MyFlags) { @@ -82,6 +100,16 @@ int my_close(File fd, myf MyFlags) } /* my_close */ +/* + Register file in my_file_info[] + + SYNOPSIS + my_register_filename() + fd + FileName + type_file_type +*/ + File my_register_filename(File fd, const char *FileName, enum file_type type_of_file, uint error_message_number, myf MyFlags) { diff --git a/mysys/my_os2cond.c b/mysys/my_os2cond.c index d930aadb2d1..e23afb89e66 100644 --- a/mysys/my_os2cond.c +++ b/mysys/my_os2cond.c @@ -26,7 +26,6 @@ #include "mysys_priv.h" #if defined(THREAD) && defined(OS2) #include <m_string.h> -//#undef getpid #include <process.h> #include <sys/timeb.h> diff --git a/mysys/my_os2dirsrch.c b/mysys/my_os2dirsrch.c index 773d6800d1e..8d1f6ddd947 100644 --- a/mysys/my_os2dirsrch.c +++ b/mysys/my_os2dirsrch.c @@ -1,4 +1,4 @@ -/* Copyright (C) Yuri Dario & 2000 MySQL AB +/* Copyright (C) Yuri Dario & 2000-2003 MySQL AB All the above parties has a full, independent copyright to the following code, including the right to use the code in any manner without any demands from the other parties. @@ -18,77 +18,79 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ + /* Win32 directory search emulation */ #if defined(OS2) -//#define _DEBUG - long _findfirst( char* path, struct _finddata_t* dos_file) { - HDIR hdir = HDIR_CREATE; - APIRET rc; - FILEFINDBUF3 buf3; - ULONG entries = 1; + HDIR hdir = HDIR_CREATE; + APIRET rc; + FILEFINDBUF3 buf3; + ULONG entries = 1; #ifdef _DEBUG - printf( "_findfirst path %s\n", path); + printf( "_findfirst path %s\n", path); #endif - memset( &buf3, 0, sizeof( buf3)); - rc = DosFindFirst( - path, /* Address of the ASCIIZ path name of the file or subdirectory to be found. */ - &hdir, /* Address of the handle associated with this DosFindFirst request. */ - FILE_NORMAL | FILE_DIRECTORY, /* Attribute value that determines the file objects to be searched for. */ - &buf3, /* Result buffer. */ - sizeof( buf3), /* The length, in bytes, of pfindbuf. */ - &entries, /* Pointer to the number of entries: */ - FIL_STANDARD); /* The level of file information required. */ + memset( &buf3, 0, sizeof( buf3)); + rc = DosFindFirst( + path, /* The ASCIIZ path name of the file or subdirectory to be found. */ + &hdir, /* The handle associated with this DosFindFirst request. */ + FILE_NORMAL | FILE_DIRECTORY, /* Attribute value that determines the file objects to be searched for. */ + &buf3, /* Result buffer. */ + sizeof( buf3), /* The length, in bytes, of pfindbuf. */ + &entries, /* Pointer to the number of entries: */ + FIL_STANDARD); /* The level of file information required. */ #ifdef _DEBUG - printf( "_findfirst rc=%d hdir=%d entries=%d->%s\n", rc, hdir, entries, buf3.achName); + printf( "_findfirst rc=%d hdir=%d entries=%d->%s\n", rc, hdir, entries, + buf3.achName); #endif - if (rc /* && entries == 0 */) - return -1; - - if (dos_file) { - memset( dos_file, 0, sizeof( struct _finddata_t)); - strcpy( dos_file->name, buf3.achName); - dos_file->size = buf3.cbFile; - dos_file->attrib = buf3.attrFile; - } - return (ULONG) hdir; + if (rc /* && entries == 0 */) + return -1; + + if (dos_file) + { + memset( dos_file, 0, sizeof( struct _finddata_t)); + strcpy( dos_file->name, buf3.achName); + dos_file->size = buf3.cbFile; + dos_file->attrib = buf3.attrFile; + } + return (ULONG) hdir; } long _findnext( long hdir, struct _finddata_t* dos_file) { - APIRET rc; - FILEFINDBUF3 buf3; - ULONG entries = 1; + APIRET rc; + FILEFINDBUF3 buf3; + ULONG entries = 1; - memset( &buf3, 0, sizeof( buf3)); - rc = DosFindNext( - hdir, - &buf3, /* Result buffer. */ - sizeof( buf3), /* The length, in bytes, of pfindbuf. */ - &entries); /* Pointer to the number of entries: */ + memset( &buf3, 0, sizeof( buf3)); + rc = DosFindNext(hdir, + &buf3, /* Result buffer. */ + sizeof( buf3), /* Length, in bytes, of pfindbuf. */ + &entries); /* Pointer to the number of entries */ #ifdef _DEBUG - printf( "_findnext rc=%d hdir=%d entries=%d->%s\n", rc, hdir, entries, buf3.achName); + printf( "_findnext rc=%d hdir=%d entries=%d->%s\n", rc, hdir, entries, + buf3.achName); #endif - if (rc /* && entries == 0 */) - return -1; - - if (dos_file) { - memset( dos_file, 0, sizeof( struct _finddata_t)); - strcpy( dos_file->name, buf3.achName); - dos_file->size = buf3.cbFile; - dos_file->attrib = buf3.attrFile; - } - return 0; + if (rc /* && entries == 0 */) + return -1; + + if (dos_file) + { + memset( dos_file, 0, sizeof( struct _finddata_t)); + strcpy( dos_file->name, buf3.achName); + dos_file->size = buf3.cbFile; + dos_file->attrib = buf3.attrFile; + } + return 0; } void _findclose( long hdir) @@ -101,82 +103,82 @@ void _findclose( long hdir) #endif } -DIR* opendir( char* path) +DIR* opendir(char* path) { - DIR* dir = (DIR*) calloc( 1, sizeof( DIR)); - char buffer[260]; - APIRET rc; - ULONG entries = 1; + DIR* dir = (DIR*) calloc(1, sizeof( DIR)); + char buffer[260]; + APIRET rc; + ULONG entries = 1; - strcpy( buffer, path); - strcat( buffer, "*.*"); + strmov(strmov(buffer, path), "*.*"); #ifdef _DEBUG - printf( "_findfirst path %s\n", buffer); + printf( "_findfirst path %s\n", buffer); #endif - dir->hdir = HDIR_CREATE; - memset( &dir->buf3, 0, sizeof( dir->buf3)); - rc = DosFindFirst( - buffer, /* Address of the ASCIIZ path name of the file or subdirectory to be found. */ - &dir->hdir, /* Address of the handle associated with this DosFindFirst request. */ - FILE_NORMAL | FILE_DIRECTORY, /* Attribute value that determines the file objects to be searched for. */ - &dir->buf3, /* Result buffer. */ - sizeof( dir->buf3), /* The length, in bytes, of pfindbuf. */ - &entries, /* Pointer to the number of entries: */ - FIL_STANDARD); /* The level of file information required. */ + dir->hdir = HDIR_CREATE; + memset( &dir->buf3, 0, sizeof( dir->buf3)); + rc = DosFindFirst( + buffer, /* Address of the ASCIIZ path name of the file or subdirectory to be found. */ + &dir->hdir, /* Address of the handle associated with this DosFindFirst request. */ + FILE_NORMAL | FILE_DIRECTORY, /* Attribute value that determines the file objects to be searched for. */ + &dir->buf3, /* Result buffer. */ + sizeof( dir->buf3), /* The length, in bytes, of pfindbuf. */ + &entries, /* Pointer to the number of entries: */ + FIL_STANDARD); /* The level of file information required. */ #ifdef _DEBUG - printf( "opendir rc=%d hdir=%d entries=%d->%s\n", rc, dir->hdir, entries, dir->buf3.achName); + printf( "opendir rc=%d hdir=%d entries=%d->%s\n", rc, dir->hdir, entries, dir->buf3.achName); #endif - if (rc /* && entries == 0 */) - return NULL; + if (rc /* && entries == 0 */) + return NULL; - return dir; + return dir; } + struct dirent* readdir( DIR* dir) { - APIRET rc; - //FILEFINDBUF3 buf3; - ULONG entries = 1; + APIRET rc; + ULONG entries = 1; - if (!dir->buf3.achName[0]) // file not found on previous query - return NULL; + if (!dir->buf3.achName[0]) /* file not found on previous query */ + return NULL; - // copy last file name - strcpy( dir->ent.d_name, dir->buf3.achName); + /* copy last file name */ + strcpy( dir->ent.d_name, dir->buf3.achName); - // query next file - memset( &dir->buf3, 0, sizeof( dir->buf3)); - rc = DosFindNext( - dir->hdir, - &dir->buf3, /* Result buffer. */ - sizeof( dir->buf3), /* The length, in bytes, of pfindbuf. */ - &entries); /* Pointer to the number of entries: */ + /* query next file */ + memset( &dir->buf3, 0, sizeof( dir->buf3)); + rc= DosFindNext( + dir->hdir, + &dir->buf3, /* Result buffer. */ + sizeof(dir->buf3), /* Length, in bytes, of pfindbuf. */ + &entries); /* Pointer to the number of entries */ #ifdef _DEBUG - printf( "_findnext rc=%d hdir=%d entries=%d->%s\n", rc, dir->hdir, entries, dir->buf3.achName); + printf( "_findnext rc=%d hdir=%d entries=%d->%s\n", rc, dir->hdir, entries, + dir->buf3.achName); #endif - if (rc /* && entries == 0 */) - strcpy( dir->buf3.achName, ""); // reset name for next query + if (rc /* && entries == 0 */) + *dir->buf3.achName= 0; /* reset name for next query */ - return &dir->ent; + return &dir->ent; } + int closedir (DIR *dir) { - APIRET rc; + APIRET rc; - rc = DosFindClose( dir->hdir); + rc = DosFindClose( dir->hdir); #ifdef _DEBUG - printf( "_findclose rc=%d hdir=%d\n", rc, dir->hdir); + printf( "_findclose rc=%d hdir=%d\n", rc, dir->hdir); #endif - free(dir); - return 0; + free(dir); + return 0; } - -#endif // OS2 +#endif /* OS2 */ diff --git a/mysys/my_os2dirsrch.h b/mysys/my_os2dirsrch.h index e894d27b576..3889f628bad 100644 --- a/mysys/my_os2dirsrch.h +++ b/mysys/my_os2dirsrch.h @@ -29,26 +29,35 @@ extern "C" { struct _finddata_t { - unsigned attrib; - //unsigned long time_create; /* -1 for FAT file systems */ - //unsigned long time_access; /* -1 for FAT file systems */ - //unsigned long time_write; - unsigned long size; - char name[260]; - //uint16 wr_date; - //uint16 wr_time; + unsigned attrib; +#ifdef NOT_USED + unsigned long time_create; /* -1 for FAT file systems */ + unsigned long time_access; /* -1 for FAT file systems */ + unsigned long time_write; +#endif + unsigned long size; + char name[260]; +#ifdef NOT_USED + uint16 wr_date; + uint16 wr_time; +#endif }; + struct dirent { - //unsigned attrib; - //unsigned long time_create; /* -1 for FAT file systems */ - //unsigned long time_access; /* -1 for FAT file systems */ - //unsigned long time_write; - //unsigned long size; - char d_name[260]; - //uint16 wr_date; - //uint16 wr_time; +#ifdef NOT_USED + unsigned attrib; + unsigned long time_create; /* -1 for FAT file systems */ + unsigned long time_access; /* -1 for FAT file systems */ + unsigned long time_write; + unsigned long size; +#endif + char d_name[260]; +#ifdef NOT_USED + uint16 wr_date; + uint16 wr_time; +#endif }; struct DIR @@ -62,16 +71,18 @@ DIR *opendir ( char *); struct dirent *readdir (DIR *); int closedir (DIR *); -//#define _A_NORMAL FILE_NORMAL -//#define _A_SUBDIR FILE_DIRECTORY -//#define _A_RDONLY FILE_READONLY +#ifdef NOT_USED +#define _A_NORMAL FILE_NORMAL +#define _A_SUBDIR FILE_DIRECTORY +#define _A_RDONLY FILE_READONLY -//long _findfirst( char*, struct _finddata_t*); -//long _findnext( long, struct _finddata_t*); -//void _findclose( long); +long _findfirst( char*, struct _finddata_t*); +long _findnext( long, struct _finddata_t*); +void _findclose( long); +#endif #ifdef __cplusplus_00 } #endif -#endif // __MY_OS2DIRSRCH2_H__ +#endif /* __MY_OS2DIRSRCH2_H__ */ diff --git a/mysys/my_os2file64.c b/mysys/my_os2file64.c index bc24c92b87f..786e083adc4 100644 --- a/mysys/my_os2file64.c +++ b/mysys/my_os2file64.c @@ -24,10 +24,11 @@ int _lock64( int fd, int locktype, my_off_t start, my_off_t length, myf MyFlags); int _sopen64( const char *name, int oflag, int shflag, int mask); -// -// this class is used to define a global c++ variable, that -// is initialized before main() gets called. -// +/* + This class is used to define a global c++ variable, that + is initialized before main() gets called. +*/ + class File64bit { public: @@ -150,7 +151,7 @@ static unsigned char const errno_tab[] = _DosSetFilePtrL = NULL; return; } - // notify success + /* notify success */ #ifdef MYSQL_SERVER printf( "WSeB 64bit file API loaded.\n"); #endif @@ -164,230 +165,230 @@ void _OS2errno( APIRET rc) errno = errno_tab[rc]; } -longlong _lseek64( int fd, longlong offset, int seektype) -{ - APIRET rc; - longlong actual; - - if (_DosSetFilePtrL) - rc = _DosSetFilePtrL( fd, offset, seektype, &actual); - else { - ULONG ulActual; - rc = DosSetFilePtr( fd, (long) offset, seektype, &ulActual); - actual = ulActual; - } - - if (!rc) - return( actual);/* NO_ERROR */ - - // set errno - _OS2errno( rc); - // seek failed - return(-1); -} -inline _SetFileLocksL(HFILE hFile, - PFILELOCKL pflUnlock, - PFILELOCKL pflLock, - ULONG timeout, - ULONG flags) +longlong _lseek64( int fd, longlong offset, int seektype) { - if (_DosSetFileLocksL) { - APIRET rc; - rc = _DosSetFileLocksL( hFile, pflUnlock, pflLock, timeout, flags); + APIRET rc; + longlong actual; - // on FAT/HPFS/LAN a INVALID_PARAMETER is returned, seems that - // only JFS can handle >2GB ranges. - if (rc != 87) - return rc; + if (_DosSetFilePtrL) + rc = _DosSetFilePtrL( fd, offset, seektype, &actual); + else + { + ULONG ulActual; + rc = DosSetFilePtr( fd, (long) offset, seektype, &ulActual); + actual = ulActual; + } - // got INVALID_PARAMETER, fallback to standard call - } + if (!rc) + return( actual); /* NO_ERROR */ - FILELOCK flUnlock = { pflUnlock->lOffset, pflUnlock->lRange }; - FILELOCK flLock = { pflLock->lOffset, pflLock->lRange }; - return DosSetFileLocks( hFile, &flUnlock, &flLock, timeout, flags); + _OS2errno( rc); /* set errno */ + return(-1); /* seek failed */ } -int _lock64( int fd, int locktype, my_off_t start, - my_off_t length, myf MyFlags) -{ - FILELOCKL LockArea = {0,0}, UnlockArea = {0,0}; - ULONG readonly = 0; - APIRET rc = -1; - - switch( locktype) { - case F_UNLCK: - UnlockArea.lOffset = start; - UnlockArea.lRange = length ? length : LONGLONG_MAX; - break; - - case F_RDLCK: - case F_WRLCK: - LockArea.lOffset = start; - LockArea.lRange = length ? length : LONGLONG_MAX; - readonly = (locktype == F_RDLCK ? 1 : 0); - break; - - default: - errno = EINVAL; - rc = -1; - break; - } - - if (MyFlags & MY_DONT_WAIT) { - - rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 0, readonly); - //printf( "fd %d, locktype %d, rc %d (dont_wait)\n", fd, locktype, rc); - if (rc == 33) { /* Lock Violation */ - - DBUG_PRINT("info",("Was locked, trying with timeout")); - rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 1 * 1000, readonly); - //printf( "fd %d, locktype %d, rc %d (dont_wait with timeout)\n", fd, locktype, rc); - } - - } else { - - while( rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 0, readonly) && (rc == 33)) { - printf("."); - DosSleep(1 * 1000); - } - //printf( "fd %d, locktype %d, rc %d (wait2)\n", fd, locktype, rc); - } - - if (!rc) - return( 0);/* NO_ERROR */ - // set errno - _OS2errno( rc); - // lock failed - return(-1); +inline APIRET _SetFileLocksL(HFILE hFile, + PFILELOCKL pflUnlock, + PFILELOCKL pflLock, + ULONG timeout, + ULONG flags) +{ + if (_DosSetFileLocksL) + { + APIRET rc; + rc = _DosSetFileLocksL( hFile, pflUnlock, pflLock, timeout, flags); + + /* + on FAT/HPFS/LAN a INVALID_PARAMETER is returned, seems that + only JFS can handle >2GB ranges. + */ + if (rc != 87) + return rc; + /* got INVALID_PARAMETER, fallback to standard call */ + } + + FILELOCK flUnlock = { pflUnlock->lOffset, pflUnlock->lRange }; + FILELOCK flLock = { pflLock->lOffset, pflLock->lRange }; + return DosSetFileLocks( hFile, &flUnlock, &flLock, timeout, flags); } -int sopen( const char *name, int oflag, int shflag, int mask) + +int _lock64( int fd, int locktype, my_off_t start, + my_off_t length, myf MyFlags) { - int fail_errno; - APIRET rc = 0; - HFILE hf = 0; - ULONG ulAction = 0; - LONGLONG cbFile = 0; - ULONG ulAttribute = FILE_NORMAL; - ULONG fsOpenFlags = 0; - ULONG fsOpenMode = 0; - - /* Extract the access mode and sharing mode bits. */ - fsOpenMode = (shflag & 0xFF) | (oflag & 0x03); - - /* Translate ERROR_OPEN_FAILED to ENOENT unless O_EXCL is set (see - below). */ - fail_errno = ENOENT; - - /* Compute `open_flag' depending on `flags'. Note that _SO_CREAT is - set for O_CREAT. */ - - if (oflag & O_CREAT) - { - if (oflag & O_EXCL) - { - fsOpenFlags = OPEN_ACTION_FAIL_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; - fail_errno = EEXIST; - } - else if (oflag & O_TRUNC) - fsOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; - else - fsOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; - - if (mask & S_IWRITE) - ulAttribute = FILE_NORMAL; - else - ulAttribute = FILE_READONLY; + FILELOCKL LockArea = {0,0}, UnlockArea = {0,0}; + ULONG readonly = 0; + APIRET rc = -1; + + switch (locktype) { + case F_UNLCK: + UnlockArea.lOffset = start; + UnlockArea.lRange = length ? length : LONGLONG_MAX; + break; + + case F_RDLCK: + case F_WRLCK: + LockArea.lOffset = start; + LockArea.lRange = length ? length : LONGLONG_MAX; + readonly = (locktype == F_RDLCK ? 1 : 0); + break; + + default: + errno = EINVAL; + rc = -1; + break; + } + + if (MyFlags & MY_DONT_WAIT) + { + rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 0, readonly); + /* printf("fd %d, locktype %d, rc %d (dont_wait)\n", fd, locktype, rc); */ + if (rc == 33) { /* Lock Violation */ + + DBUG_PRINT("info",("Was locked, trying with timeout")); + rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 1 * 1000, readonly); + /* printf( "fd %d, locktype %d, rc %d (dont_wait with timeout)\n", fd, locktype, rc); */ + } + } + else + { + while (rc = _SetFileLocksL( fd, &UnlockArea, &LockArea, 0, readonly) && + (rc == 33)) + { + printf("."); + DosSleep(1 * 1000); + } + /* printf( "fd %d, locktype %d, rc %d (wait2)\n", fd, locktype, rc); */ + } + if (!rc) + return(0); /* NO_ERROR */ + _OS2errno( rc); /* set errno */ + return(-1); /* lock failed */ +} - } - else if (oflag & O_TRUNC) - fsOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW; - else - fsOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW; - - /* Try to open the file and handle errors. */ - if (_DosOpenL) - rc = _DosOpenL( name, &hf, &ulAction, cbFile, - ulAttribute, fsOpenFlags, fsOpenMode, NULL); - else - rc = DosOpen( name, &hf, &ulAction, (LONG) cbFile, - ulAttribute, fsOpenFlags, fsOpenMode, NULL); - - if (rc == ERROR_OPEN_FAILED) - { - errno = fail_errno; - return -1; - } - if (rc != 0) - { - // set errno - _OS2errno( rc); - return -1; - } - if (oflag & O_APPEND) - _lseek64( hf, 0L, SEEK_END); +int sopen(const char *name, int oflag, int shflag, int mask) +{ + int fail_errno; + APIRET rc = 0; + HFILE hf = 0; + ULONG ulAction = 0; + LONGLONG cbFile = 0; + ULONG ulAttribute = FILE_NORMAL; + ULONG fsOpenFlags = 0; + ULONG fsOpenMode = 0; + + /* Extract the access mode and sharing mode bits. */ + fsOpenMode = (shflag & 0xFF) | (oflag & 0x03); + + /* + Translate ERROR_OPEN_FAILED to ENOENT unless O_EXCL is set (see + below). + */ + fail_errno = ENOENT; + + /* + Compute `open_flag' depending on `flags'. Note that _SO_CREAT is + set for O_CREAT. + */ + + if (oflag & O_CREAT) + { + if (oflag & O_EXCL) + { + fsOpenFlags = OPEN_ACTION_FAIL_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; + fail_errno = EEXIST; + } + else if (oflag & O_TRUNC) + fsOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; + else + fsOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW; + + if (mask & S_IWRITE) + ulAttribute = FILE_NORMAL; + else + ulAttribute = FILE_READONLY; + + } + else if (oflag & O_TRUNC) + fsOpenFlags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW; + else + fsOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW; - return hf; + /* Try to open the file and handle errors. */ + if (_DosOpenL) + rc = _DosOpenL( name, &hf, &ulAction, cbFile, + ulAttribute, fsOpenFlags, fsOpenMode, NULL); + else + rc = DosOpen( name, &hf, &ulAction, (LONG) cbFile, + ulAttribute, fsOpenFlags, fsOpenMode, NULL); + + if (rc == ERROR_OPEN_FAILED) + { + errno = fail_errno; + return -1; + } + if (rc != 0) + { + _OS2errno( rc); /* set errno */ + return -1; + } + if (oflag & O_APPEND) + _lseek64( hf, 0L, SEEK_END); + return hf; } -int read( int fd, void *buffer, unsigned int count) -{ - APIRET rc; - ULONG actual; - rc = DosRead( fd, (PVOID) buffer, count, &actual); +int read(int fd, void *buffer, unsigned int count) +{ + APIRET rc; + ULONG actual; - if (!rc) - return( actual);/* NO_ERROR */ + rc= DosRead( fd, (PVOID) buffer, count, &actual); - // set errno - _OS2errno( rc); - // write failed - return(-1); + if (!rc) + return( actual); /* NO_ERROR */ + _OS2errno( rc); /* set errno */ + return(-1); /* read failed */ } -int write( int fd, const void *buffer, unsigned int count) -{ - APIRET rc; - ULONG actual; - rc = DosWrite( fd, (PVOID) buffer, count, &actual); +int write(int fd, const void *buffer, unsigned int count) +{ + APIRET rc; + ULONG actual; - if (!rc) - return( actual);/* NO_ERROR */ + rc = DosWrite( fd, (PVOID) buffer, count, &actual); - // set errno - _OS2errno( rc); - // write failed - return(-1); + if (!rc) + return( actual); /* NO_ERROR */ + _OS2errno( rc); /* set errno */ + return(-1); /* write failed */ } -int close( int fd) -{ - APIRET rc; - ULONG actual; - rc = DosClose( fd); +int close( int fd) +{ + APIRET rc; + ULONG actual; - if (!rc) - return( 0);/* NO_ERROR */ + rc = DosClose( fd); - // set errno - _OS2errno( rc); - // write failed - return(-1); + if (!rc) + return( 0); /* NO_ERROR */ + _OS2errno( rc); /* set errno */ + return(-1); /* close failed */ } -inline int open( const char *name, int oflag) + +int open( const char *name, int oflag) { return sopen( name, oflag, OPEN_SHARE_DENYNONE, S_IREAD | S_IWRITE); } -inline int open( const char *name, int oflag, int mask) + +int open( const char *name, int oflag, int mask) { return sopen( name, oflag, OPEN_SHARE_DENYNONE, mask); } diff --git a/mysys/my_os2mutex.c b/mysys/my_os2mutex.c index 718aa9f2932..5010d6e8dd5 100644 --- a/mysys/my_os2mutex.c +++ b/mysys/my_os2mutex.c @@ -36,63 +36,48 @@ #include <stdlib.h> #include <errno.h> #ifdef _THREAD_SAFE -//#include <pthread.h> -//#include "pthread_private.h" int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutex_attr) { - APIRET rc = 0; - - rc = DosCreateMutexSem(NULL,mutex,0,0); - - /* Return the completion status: */ - return (0); + (void) DosCreateMutexSem(NULL,mutex,0,0); + return (0); /* Return the completion status: */ } + int pthread_mutex_destroy(pthread_mutex_t * mutex) { - APIRET rc = 0; - + APIRET rc; - do { - rc = DosCloseMutexSem(*mutex); - if (rc == 301) DosReleaseMutexSem(*mutex); - } while (rc == 301); + do + { + rc = DosCloseMutexSem(*mutex); + if (rc == 301) DosReleaseMutexSem(*mutex); + } while (rc == 301); - *mutex = 0; - - /* Return the completion status: */ - return (0); + *mutex = 0; + return (0); /* Return the completion status: */ } int pthread_mutex_lock(pthread_mutex_t * mutex) { - int ret = 0; - int status = 0; - APIRET rc = 0; + APIRET rc; - rc = DosRequestMutexSem(*mutex,SEM_INDEFINITE_WAIT); - if (rc) - return(EINVAL); - /* Return the completion status: */ - return (0); + rc = DosRequestMutexSem(*mutex,SEM_INDEFINITE_WAIT); + if (rc) + return(EINVAL); + return (0); /* Return the completion status: */ } + int pthread_mutex_unlock(pthread_mutex_t * mutex) { - int ret = 0; - APIRET rc = 0; - int status; - - rc = DosReleaseMutexSem(*mutex); - - /* Return the completion status: */ - return (0); + (void) DosReleaseMutexSem(*mutex); + return (0); /* Return the completion status: */ } #endif diff --git a/mysys/my_os2thread.c b/mysys/my_os2thread.c index 6f196f43d75..0696f480a91 100644 --- a/mysys/my_os2thread.c +++ b/mysys/my_os2thread.c @@ -61,10 +61,9 @@ static pthread_handler_decl(pthread_start,param) win_pthread_self=((struct pthread_map *) param)->pthreadself; pthread_mutex_unlock(&THR_LOCK_thread); free((char*) param); /* Free param from create */ - //pthread_exit((void*) (*func)(func_param)); + /* pthread_exit((void*) (*func)(func_param)); */ (*func)(func_param); DBUG_RETURN(0); - //return 0; /* Safety */ } diff --git a/mysys/my_os2tls.c b/mysys/my_os2tls.c index 1598fa34e2b..f7cf3b09283 100644 --- a/mysys/my_os2tls.c +++ b/mysys/my_os2tls.c @@ -30,115 +30,122 @@ PULONG tls_storage; /* TLS local storage */ DWORD tls_bits[2]; /* TLS in-use bits */ pthread_mutex_t tls_mutex; /* TLS mutex for in-use bits */ + DWORD TlsAlloc( void) { - DWORD index = -1; - DWORD mask, tibidx; - int i; - - if (tls_storage == NULL) { - - APIRET rc; - - // allocate memory for TLS storage - rc = DosAllocThreadLocalMemory( 1, &tls_storage); - if (rc) { - fprintf( stderr, "DosAllocThreadLocalMemory error: return code = %u\n", rc); - } - - // create a mutex - if (pthread_mutex_init( &tls_mutex, NULL)) - fprintf( stderr, "Failed to init TLS mutex\n"); - } - - pthread_mutex_lock( &tls_mutex); - - tibidx = 0; - if (tls_bits[0] == 0xFFFFFFFF) { - if (tls_bits[1] == 0xFFFFFFFF) { - fprintf( stderr, "tid#%d, no more TLS bits available\n", _threadid); - pthread_mutex_unlock( &tls_mutex); - return -1; - } - tibidx = 1; - } - for( i=0; i<32; i++) { - mask = (1 << i); - if ((tls_bits[ tibidx] & mask) == 0) { - tls_bits[ tibidx] |= mask; - index = (tibidx*32) + i; - break; - } - } - tls_storage[index] = 0; - - pthread_mutex_unlock( &tls_mutex); - - //fprintf( stderr, "tid#%d, TlsAlloc index %d\n", _threadid, index); - - return index; + DWORD index = -1; + DWORD mask, tibidx; + int i; + + if (tls_storage == NULL) + { + + APIRET rc; + + /* allocate memory for TLS storage */ + rc = DosAllocThreadLocalMemory( 1, &tls_storage); + if (rc) + fprintf( stderr, "DosAllocThreadLocalMemory error: return code = %u\n", + rc); + /* create a mutex */ + if (pthread_mutex_init( &tls_mutex, NULL)) + fprintf( stderr, "Failed to init TLS mutex\n"); + } + + pthread_mutex_lock( &tls_mutex); + + tibidx = 0; + if (tls_bits[0] == 0xFFFFFFFF) + { + if (tls_bits[1] == 0xFFFFFFFF) + { + fprintf( stderr, "tid#%d, no more TLS bits available\n", _threadid); + pthread_mutex_unlock( &tls_mutex); + return -1; + } + tibidx = 1; + } + + for (i=0; i<32; i++) + { + mask = (1 << i); + if ((tls_bits[ tibidx] & mask) == 0) + { + tls_bits[ tibidx] |= mask; + index = (tibidx*32) + i; + break; + } + } + tls_storage[index] = 0; + + pthread_mutex_unlock( &tls_mutex); + /* fprintf( stderr, "tid#%d, TlsAlloc index %d\n", _threadid, index); */ + return index; } -BOOL TlsFree( DWORD index) +BOOL TlsFree( DWORD index) { - int tlsidx; - DWORD mask; - - if (index >= TLS_MINIMUM_AVAILABLE) - return NULL; - - pthread_mutex_lock( &tls_mutex); - - tlsidx = 0; - if (index > 32) { - tlsidx++; - } - mask = (1 << index); - if (tls_bits[ tlsidx] & mask) { - tls_bits[tlsidx] &= ~mask; - tls_storage[index] = 0; - pthread_mutex_unlock( &tls_mutex); - return TRUE; - } + int tlsidx; + DWORD mask; - pthread_mutex_unlock( &tls_mutex); - return FALSE; -} + if (index >= TLS_MINIMUM_AVAILABLE) + return NULL; + pthread_mutex_lock( &tls_mutex); -PVOID TlsGetValue( DWORD index) -{ - if (index >= TLS_MINIMUM_AVAILABLE) - return NULL; - - // verify if memory has been allocated for this thread - if (*tls_storage == NULL) { - // allocate memory for indexes - *tls_storage = (ULONG)calloc( TLS_MINIMUM_AVAILABLE, sizeof(int)); - //fprintf( stderr, "tid#%d, tls_storage %x\n", _threadid, *tls_storage); - } - - ULONG* tls_array = (ULONG*) *tls_storage; - return (PVOID) tls_array[ index]; + tlsidx = 0; + if (index > 32) + tlsidx++; + + mask = (1 << index); + if (tls_bits[ tlsidx] & mask) + { + tls_bits[tlsidx] &= ~mask; + tls_storage[index] = 0; + pthread_mutex_unlock( &tls_mutex); + return TRUE; + } + + pthread_mutex_unlock( &tls_mutex); + return FALSE; } -BOOL TlsSetValue( DWORD index, PVOID val) -{ - // verify if memory has been allocated for this thread - if (*tls_storage == NULL) { - // allocate memory for indexes - *tls_storage = (ULONG)calloc( TLS_MINIMUM_AVAILABLE, sizeof(int)); - //fprintf( stderr, "tid#%d, tls_storage %x\n", _threadid, *tls_storage); - } +PVOID TlsGetValue( DWORD index) +{ + if (index >= TLS_MINIMUM_AVAILABLE) + return NULL; + + /* verify if memory has been allocated for this thread */ + if (*tls_storage == NULL) + { + /* allocate memory for indexes */ + *tls_storage = (ULONG)calloc( TLS_MINIMUM_AVAILABLE, sizeof(int)); + /* fprintf(stderr, "tid#%d, tls_storage %x\n", _threadid, *tls_storage); */ + } + + ULONG* tls_array = (ULONG*) *tls_storage; + return (PVOID) tls_array[index]; +} - if (index >= TLS_MINIMUM_AVAILABLE) - return FALSE; - ULONG* tls_array = (ULONG*) *tls_storage; - //fprintf( stderr, "tid#%d, TlsSetValue array %08x index %d -> %08x (old)\n", _threadid, tls_array, index, tls_array[ index]); - tls_array[ index] = (ULONG) val; - //fprintf( stderr, "tid#%d, TlsSetValue array %08x index %d -> %08x\n", _threadid, tls_array, index, val); +BOOL TlsSetValue( DWORD index, PVOID val) +{ - return TRUE; + /* verify if memory has been allocated for this thread */ + if (*tls_storage == NULL) + { + /* allocate memory for indexes */ + *tls_storage = (ULONG)calloc( TLS_MINIMUM_AVAILABLE, sizeof(int)); + /* fprintf(stderr, "tid#%d, tls_storage %x\n", _threadid, *tls_storage); */ + } + + if (index >= TLS_MINIMUM_AVAILABLE) + return FALSE; + + ULONG* tls_array = (ULONG*) *tls_storage; + /* fprintf( stderr, "tid#%d, TlsSetValue array %08x index %d -> %08x (old)\n", _threadid, tls_array, index, tls_array[ index]); */ + tls_array[ index] = (ULONG) val; + /* fprintf( stderr, "tid#%d, TlsSetValue array %08x index %d -> %08x\n", _threadid, tls_array, index, val); */ + return TRUE; } diff --git a/mysys/my_pread.c b/mysys/my_pread.c index 5c7d0be5854..661ef48ab3e 100644 --- a/mysys/my_pread.c +++ b/mysys/my_pread.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/my_pthread.c b/mysys/my_pthread.c index ecd3631f59d..7ad4852558e 100644 --- a/mysys/my_pthread.c +++ b/mysys/my_pthread.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000-2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Functions to get threads more portable */ @@ -394,7 +393,7 @@ int pthread_signal(int sig, void (*func)()) #undef pthread_cond_wait #undef pthread_cond_timedwait #undef pthread_cond_t - +#undef pthread_attr_getstacksize /***************************************************************************** ** Patches for AIX and DEC OSF/1 3.2 @@ -427,6 +426,19 @@ int my_pthread_cond_init(pthread_cond_t *mp, const pthread_condattr_t *attr) #endif +#ifdef __NETWARE__ +/* NetWare does not re-acquire the lock if the condition fails */ +int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + struct timespec *abstime) +{ + int err= pthread_cond_timedwait(cond, mutex, abstime); + if (err) + pthread_mutex_lock(mutex); + return err; +} +#endif /* __NETWARE__ */ + + /***************************************************************************** Patches for HPUX We need these because the pthread_mutex.. code returns -1 on error, @@ -437,7 +449,7 @@ int my_pthread_cond_init(pthread_cond_t *mp, const pthread_condattr_t *attr) this has to be added here. ****************************************************************************/ -#if defined(HPUX) || defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) +#if defined(HPUX10) || defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *abstime) @@ -454,6 +466,15 @@ int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, } #endif +#if defined(HPUX10) + +void my_pthread_attr_getstacksize(pthread_attr_t *connection_attrib, + size_t *stack_size) +{ + *stack_size= pthread_attr_getstacksize(*connection_attrib); +} +#endif + #ifdef HAVE_POSIX1003_4a_MUTEX /* diff --git a/mysys/my_quick.c b/mysys/my_quick.c index 6151d5037ae..44ed3fc0b2c 100644 --- a/mysys/my_quick.c +++ b/mysys/my_quick.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Quicker interface to read & write. Used with my_nosys.h */ diff --git a/mysys/my_read.c b/mysys/my_read.c index b317630f4bd..b7621ac99eb 100644 --- a/mysys/my_read.c +++ b/mysys/my_read.c @@ -1,37 +1,46 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" #include <errno.h> - /* Read a chunk of bytes from a file */ +/* + Read a chunk of bytes from a file with retry's if needed + + The parameters are: + File descriptor + Buffer to hold at least Count bytes + Bytes to read + Flags on what to do on error + + Return: + -1 on error + 0 if flag has bits MY_NABP or MY_FNABP set + N number of bytes read. +*/ uint my_read(File Filedes, byte *Buffer, uint Count, myf MyFlags) - /* File descriptor */ - /* Buffer must be at least count bytes */ - /* Max number of bytes returnd */ - /* Flags on what to do on error */ { - uint readbytes; + uint readbytes,save_count; DBUG_ENTER("my_read"); DBUG_PRINT("my",("Fd: %d Buffer: %lx Count: %u MyFlags: %d", Filedes, Buffer, Count, MyFlags)); + save_count=Count; for (;;) { @@ -54,12 +63,21 @@ uint my_read(File Filedes, byte *Buffer, uint Count, myf MyFlags) my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG), my_filename(Filedes),my_errno); } - if ((int) readbytes == -1 || (MyFlags & (MY_FNABP | MY_NABP))) + if ((int) readbytes == -1 || + ((MyFlags & (MY_FNABP | MY_NABP)) && !(MyFlags & MY_FULL_IO))) DBUG_RETURN(MY_FILE_ERROR); /* Return with error */ + if (readbytes > 0 && (MyFlags & MY_FULL_IO)) + { + Buffer+=readbytes; + Count-=readbytes; + continue; + } } if (MyFlags & (MY_NABP | MY_FNABP)) readbytes=0; /* Ok on read */ + else if (MyFlags & MY_FULL_IO) + readbytes=save_count; break; } DBUG_RETURN(readbytes); diff --git a/mysys/my_realloc.c b/mysys/my_realloc.c index 7ab75d47619..49d96c2eb4f 100644 --- a/mysys/my_realloc.c +++ b/mysys/my_realloc.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef SAFEMALLOC /* We don't need SAFEMALLOC here */ #undef SAFEMALLOC diff --git a/mysys/my_redel.c b/mysys/my_redel.c index 16ec77394d2..9ba03cd9526 100644 --- a/mysys/my_redel.c +++ b/mysys/my_redel.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES /* sys/types is included */ #include "mysys_priv.h" @@ -24,7 +23,7 @@ #include <sys/utime.h> #elif defined(HAVE_UTIME_H) #include <utime.h> -#elif !defined(HPUX) +#elif !defined(HPUX10) struct utimbuf { time_t actime; time_t modtime; @@ -91,7 +90,7 @@ int my_copystat(const char *from, const char *to, int MyFlags) return 1; VOID(chmod(to, statbuf.st_mode & 07777)); /* Copy modes */ -#if !defined(MSDOS) && !defined(__WIN__) && !defined(__EMX__) && !defined(OS2) +#if !defined(MSDOS) && !defined(__WIN__) && !defined(__EMX__) && !defined(OS2) && !defined(__NETWARE__) if (statbuf.st_nlink > 1 && MyFlags & MY_LINK_WARNING) { if (MyFlags & MY_LINK_WARNING) diff --git a/mysys/my_rename.c b/mysys/my_rename.c index 1b7f434e792..d4f99e83247 100644 --- a/mysys/my_rename.c +++ b/mysys/my_rename.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define USES_TYPES #include "mysys_priv.h" diff --git a/mysys/my_seek.c b/mysys/my_seek.c index 7c4da4f6010..177a5cee953 100644 --- a/mysys/my_seek.c +++ b/mysys/my_seek.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" @@ -26,7 +25,8 @@ my_off_t my_seek(File fd, my_off_t pos, int whence, reg1 os_off_t newpos; DBUG_ENTER("my_seek"); DBUG_PRINT("my",("Fd: %d Hpos: %lu Pos: %lu Whence: %d MyFlags: %d", - fd, ((ulonglong) pos) >> 32, (ulong) pos, whence, MyFlags)); + fd, (ulong) (((ulonglong) pos) >> 32), (ulong) pos, + whence, MyFlags)); newpos=lseek(fd, pos, whence); if (newpos == (os_off_t) -1) { @@ -34,6 +34,10 @@ my_off_t my_seek(File fd, my_off_t pos, int whence, DBUG_PRINT("error",("lseek: %lu, errno: %d",newpos,errno)); DBUG_RETURN(MY_FILEPOS_ERROR); } + if ((my_off_t) newpos != pos) + { + DBUG_PRINT("exit",("pos: %lu", (ulong) newpos)); + } DBUG_RETURN((my_off_t) newpos); } /* my_seek */ @@ -53,6 +57,6 @@ my_off_t my_tell(File fd, myf MyFlags __attribute__((unused))) #endif if (pos == (os_off_t) -1) my_errno=errno; - DBUG_PRINT("exit",("pos: %lu",pos)); + DBUG_PRINT("exit",("pos: %lu", (ulong) pos)); DBUG_RETURN((my_off_t) pos); } /* my_tell */ diff --git a/mysys/my_semaphore.c b/mysys/my_semaphore.c new file mode 100644 index 00000000000..2dd224cd99e --- /dev/null +++ b/mysys/my_semaphore.c @@ -0,0 +1,104 @@ +/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + Simple implementation of semaphores, needed to compile MySQL on systems + that doesn't support semaphores. +*/ + +#include <my_global.h> +#include <my_semaphore.h> +#include <errno.h> + +#if !defined(__WIN__) && !defined(HAVE_SEMAPHORE_H) + +int sem_init(sem_t * sem, int pshared, uint value) +{ + sem->count=value; + pthread_cond_init(&sem->cond, 0); + pthread_mutex_init(&sem->mutex, 0); + return 0; +} + +int sem_destroy(sem_t * sem) +{ + int err1,err2; + err1=pthread_cond_destroy(&sem->cond); + err2=pthread_mutex_destroy(&sem->mutex); + if (err1 || err2) + { + errno=err1 ? err1 : err2; + return -1; + } + return 0; +} + +int sem_wait(sem_t * sem) +{ + if ((errno=pthread_mutex_lock(&sem->mutex))) + return -1; + while (!sem->count) + pthread_cond_wait(&sem->cond, &sem->mutex); + if (errno) + return -1; + sem->count--; /* mutex is locked here */ + pthread_mutex_unlock(&sem->mutex); + return 0; +} + +int sem_trywait(sem_t * sem) +{ + if ((errno=pthread_mutex_lock(&sem->mutex))) + return -1; + if (sem->count) + sem->count--; + else + errno=EAGAIN; + pthread_mutex_unlock(&sem->mutex); + return errno ? -1 : 0; +} + + +int sem_post(sem_t * sem) +{ + if ((errno=pthread_mutex_lock(&sem->mutex))) + return -1; + sem->count++; + pthread_mutex_unlock(&sem->mutex); /* does it really matter what to do */ + pthread_cond_signal(&sem->cond); /* first: x_unlock or x_signal ? */ + return 0; +} + +int sem_post_multiple(sem_t * sem, uint count) +{ + if ((errno=pthread_mutex_lock(&sem->mutex))) + return -1; + sem->count+=count; + pthread_mutex_unlock(&sem->mutex); /* does it really matter what to do */ + pthread_cond_broadcast(&sem->cond); /* first: x_unlock or x_broadcast ? */ + return 0; +} + +int sem_getvalue(sem_t * sem, uint *sval) +{ + if ((errno=pthread_mutex_lock(&sem->mutex))) + return -1; + *sval=sem->count; + pthread_mutex_unlock(&sem->mutex); + return 0; +} + +#endif /* !defined(__WIN__) && !defined(HAVE_SEMAPHORE_H) */ diff --git a/mysys/my_sleep.c b/mysys/my_sleep.c new file mode 100644 index 00000000000..3de2d2abd13 --- /dev/null +++ b/mysys/my_sleep.c @@ -0,0 +1,38 @@ +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* Wait a given number of microseconds */ + +#include "mysys_priv.h" +#include <m_string.h> + +void my_sleep(ulong m_seconds) +{ +#ifdef __NETWARE__ + delay(m_seconds/1000+1); +#elif defined(OS2) + DosSleep(m_seconds/1000+1); +#elif defined(HAVE_SELECT) + struct timeval t; + t.tv_sec= m_seconds / 1000000L; + t.tv_usec= m_seconds % 1000000L; + select(0,0,0,0,&t); /* sleep */ +#else + uint sec= (uint) (m_seconds / 1000000L); + ulong start= (ulong) time((time_t*) 0); + while ((ulong) time((time_t*) 0) < start+sec); +#endif +} diff --git a/mysys/my_static.c b/mysys/my_static.c index 72bf7207efa..bbf7582a454 100644 --- a/mysys/my_static.c +++ b/mysys/my_static.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Static variables for mysys library. All definied here for easy making of @@ -83,6 +82,13 @@ struct remember *pRememberRoot = NULL; int volatile my_have_got_alarm=0; /* declare variable to reset */ ulong my_time_to_wait_for_lock=2; /* In seconds */ + /* + We need to have this define here as otherwise linking will fail + on OSF1 when compiling --without-raid --with-debug + */ + +const char *raid_type_string[]={"none","striped"}; + /* from errors.c */ #ifdef SHARED_LIBRARY char * NEAR globerrs[GLOBERRS]; /* my_error_messages is here */ diff --git a/mysys/my_static.h b/mysys/my_static.h index e139b0ec127..c1893f4074f 100644 --- a/mysys/my_static.h +++ b/mysys/my_static.h @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Static variables for mysys library. All definied here for easy making of @@ -33,13 +32,22 @@ struct st_remember { sig_handler (*func)(int number); }; -struct irem { - struct remember *_pNext; /* Linked list of structures */ - struct remember *_pPrev; /* Other link */ - my_string _sFileName; /* File in which memory was new'ed */ - uint _uLineNum; /* Line number in above file */ - uint _uDataSize; /* Size requested */ - long _lSpecialValue; /* Underrun marker value */ +/* + The size of the following structure MUST be dividable by 8 to not cause + alignment problems on some cpu's +*/ + +struct irem +{ + struct remember *_pNext; /* Linked list of structures */ + struct remember *_pPrev; /* Other link */ + char *_sFileName; /* File in which memory was new'ed */ + uint32 _uLineNum; /* Line number in above file */ + uint32 _uDataSize; /* Size requested */ +#if SIZEOF_CHARP == 8 + long _filler; /* For alignment */ +#endif + long _lSpecialValue; /* Underrun marker value */ }; struct remember { diff --git a/mysys/my_symlink.c b/mysys/my_symlink.c index 65d165fc026..e287930ff06 100644 --- a/mysys/my_symlink.c +++ b/mysys/my_symlink.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" @@ -116,11 +115,15 @@ int my_realpath(char *to, const char *filename, myf MyFlags) (!lstat(filename,&stat_buff) && S_ISLNK(stat_buff.st_mode))) { char *ptr; + DBUG_PRINT("info",("executing realpath")); if ((ptr=realpath(filename,buff))) + { strmake(to,ptr,FN_REFLEN-1); + } else { /* Realpath didn't work; Use original name */ + DBUG_PRINT("error",("realpath failed with errno: %d", errno)); my_errno=errno; if (MyFlags & MY_WME) my_error(EE_REALPATH, MYF(0), filename, my_errno); diff --git a/mysys/my_symlink2.c b/mysys/my_symlink2.c index f1e90abaa9b..68b034bec5c 100644 --- a/mysys/my_symlink2.c +++ b/mysys/my_symlink2.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Advanced symlink handling. @@ -23,6 +22,7 @@ */ #include "mysys_priv.h" +#include "mysys_err.h" #include <m_string.h> File my_create_with_symlink(const char *linkname, const char *filename, @@ -30,11 +30,27 @@ File my_create_with_symlink(const char *linkname, const char *filename, { File file; int tmp_errno; + /* Test if we should create a link */ + int create_link=(linkname && strcmp(linkname,filename)); DBUG_ENTER("my_create_with_symlink"); + + if (!(MyFlags & MY_DELETE_OLD)) + { + if (!access(filename,F_OK)) + { + my_error(EE_CANTCREATEFILE, MYF(0), filename, EEXIST); + DBUG_RETURN(-1); + } + if (create_link && !access(linkname,F_OK)) + { + my_error(EE_CANTCREATEFILE, MYF(0), linkname, EEXIST); + DBUG_RETURN(-1); + } + } + if ((file=my_create(filename, createflags, access_flags, MyFlags)) >= 0) { - /* Test if we should create a link */ - if (linkname && strcmp(linkname,filename)) + if (create_link) { /* Delete old link/file */ if (MyFlags & MY_DELETE_OLD) diff --git a/mysys/my_tempnam.c b/mysys/my_tempnam.c index d4979fcaa6e..d079b9f66a5 100644 --- a/mysys/my_tempnam.c +++ b/mysys/my_tempnam.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB +/* Copyright (C) 2000 MySQL AB - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This library is distributed in the hope that it will be useful, + This program 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 - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU Library 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 */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* This function is only used by some old ISAM code. @@ -39,7 +38,7 @@ #endif #ifdef HAVE_TEMPNAM -#if !defined( MSDOS) && !defined(OS2) +#if !defined( MSDOS) && !defined(OS2) && !defined(__NETWARE__) extern char **environ; #endif #endif @@ -105,24 +104,30 @@ my_string my_tempnam(const char *dir, const char *pfx, dir=temp; } #ifdef OS2 - // changing environ variable doesn't work with VACPP + /* changing environ variable doesn't work with VACPP */ char buffer[256]; sprintf( buffer, "TMP=%s", dir); - // remove ending backslash + /* remove ending backslash */ if (buffer[strlen(buffer)-1] == '\\') buffer[strlen(buffer)-1] = '\0'; putenv( buffer); -#else +#elif !defined(__NETWARE__) old_env=(char**)environ; if (dir) { /* Don't use TMPDIR if dir is given */ - environ=(const char**)temp_env; /* May give warning */ + /* + The following strange cast is required because the IBM compiler on AIX + doesn't allow us to cast the value of environ. + The cast of environ is needed as some systems doesn't allow us to + update environ with a char ** pointer. (const mismatch) + */ + (*(char***) &environ)=(char**) temp_env; temp_env[0]=0; } #endif res=tempnam((char*) dir,(my_string) pfx); /* Use stand. dir with prefix */ -#ifndef OS2 - environ=(const char**)old_env; /* May give warning */ +#if !defined(OS2) && !defined(__NETWARE__) + (*(char***) &environ)=(char**) old_env; #endif if (!res) DBUG_PRINT("error",("Got error: %d from tempnam",errno)); diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.c index 2782576d8cd..59466083b28 100644 --- a/mysys/my_thr_init.c +++ b/mysys/my_thr_init.c @@ -1,23 +1,22 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* -** Functions to handle initializating and allocationg of all mysys & debug -** thread variables. + Functions to handle initializating and allocationg of all mysys & debug + thread variables. */ #include "mysys_priv.h" @@ -45,10 +44,6 @@ pthread_mutexattr_t my_fast_mutexattr; pthread_mutexattr_t my_errchk_mutexattr; #endif -/* FIXME Note. TlsAlloc does not set an auto destructor, so - the function my_thread_global_free must be called from - somewhere before final exit of the library */ - my_bool my_thread_global_init(void) { if (pthread_key_create(&THR_KEY_mysys,free)) @@ -60,7 +55,7 @@ my_bool my_thread_global_init(void) pthread_mutexattr_init(&my_fast_mutexattr); pthread_mutexattr_setkind_np(&my_fast_mutexattr,PTHREAD_MUTEX_ADAPTIVE_NP); #endif -#ifdef PPTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP +#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP pthread_mutexattr_init(&my_errchk_mutexattr); pthread_mutexattr_setkind_np(&my_errchk_mutexattr, PTHREAD_MUTEX_ERRORCHECK_NP); @@ -87,6 +82,7 @@ my_bool my_thread_global_init(void) return my_thread_init(); } + void my_thread_global_end(void) { #if defined(USE_TLS) @@ -98,6 +94,18 @@ void my_thread_global_end(void) #ifdef PPTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP pthread_mutexattr_destroy(&my_errchk_mutexattr); #endif + pthread_mutex_destroy(&THR_LOCK_malloc); + pthread_mutex_destroy(&THR_LOCK_open); + pthread_mutex_destroy(&THR_LOCK_keycache); + pthread_mutex_destroy(&THR_LOCK_lock); + pthread_mutex_destroy(&THR_LOCK_isam); + pthread_mutex_destroy(&THR_LOCK_myisam); + pthread_mutex_destroy(&THR_LOCK_heap); + pthread_mutex_destroy(&THR_LOCK_net); + pthread_mutex_destroy(&THR_LOCK_charset); +#ifndef HAVE_LOCALTIME_R + pthread_mutex_destroy(&LOCK_localtime_r); +#endif #ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_destroy(&LOCK_gethostbyname_r); #endif @@ -115,50 +123,62 @@ static long thread_id=0; my_bool my_thread_init(void) { struct st_my_thread_var *tmp; + my_bool error=0; + +#ifdef EXTRA_DEBUG_THREADS + fprintf(stderr,"my_thread_init(): thread_id=%ld\n",pthread_self()); +#endif #if !defined(__WIN__) || defined(USE_TLS) || ! defined(SAFE_MUTEX) pthread_mutex_lock(&THR_LOCK_lock); #endif + #if !defined(__WIN__) || defined(USE_TLS) if (my_pthread_getspecific(struct st_my_thread_var *,THR_KEY_mysys)) { - pthread_mutex_unlock(&THR_LOCK_lock); - return 0; /* Safequard */ +#ifdef EXTRA_DEBUG_THREADS + fprintf(stderr,"my_thread_init() called more than once in thread %ld\n", + pthread_self()); +#endif + goto end; } - /* We must have many calloc() here because these are freed on - pthread_exit */ - if (!(tmp=(struct st_my_thread_var *) - calloc(1,sizeof(struct st_my_thread_var)))) + if (!(tmp= (struct st_my_thread_var *) calloc(1, sizeof(*tmp)))) { - pthread_mutex_unlock(&THR_LOCK_lock); - return 1; + error= 1; + goto end; } pthread_setspecific(THR_KEY_mysys,tmp); #else - if (THR_KEY_mysys.id) /* Already initialized */ - { -#if !defined(__WIN__) || defined(USE_TLS) || ! defined(SAFE_MUTEX) - pthread_mutex_unlock(&THR_LOCK_lock); -#endif - return 0; - } + /* + Skip initialization if the thread specific variable is already initialized + */ + if (THR_KEY_mysys.id) + goto end; tmp= &THR_KEY_mysys; #endif tmp->id= ++thread_id; pthread_mutex_init(&tmp->mutex,MY_MUTEX_INIT_FAST); pthread_cond_init(&tmp->suspend, NULL); + +end: #if !defined(__WIN__) || defined(USE_TLS) || ! defined(SAFE_MUTEX) pthread_mutex_unlock(&THR_LOCK_lock); #endif - return 0; + return error; } + void my_thread_end(void) { struct st_my_thread_var *tmp=my_thread_var; +#ifdef EXTRA_DEBUG_THREADS + fprintf(stderr,"my_thread_end(): tmp=%p,thread_id=%ld\n", + tmp,pthread_self()); +#endif if (tmp) { #if !defined(DBUG_OFF) + /* tmp->dbug is allocated inside DBUG library */ if (tmp->dbug) { free(tmp->dbug); @@ -173,6 +193,7 @@ void my_thread_end(void) free(tmp); #endif } + /* The following free has to be done, even if my_thread_var() is 0 */ #if (!defined(__WIN__) && !defined(OS2)) || defined(USE_TLS) pthread_setspecific(THR_KEY_mysys,0); #endif @@ -193,8 +214,9 @@ struct st_my_thread_var *_my_thread_var(void) return tmp; } + /**************************************************************************** -** Get name of current thread. + Get name of current thread. ****************************************************************************/ #define UNKNOWN_THREAD -1 diff --git a/mysys/my_vsnprintf.c b/mysys/my_vsnprintf.c index ab8edec4d7b..7490ab4a9f2 100644 --- a/mysys/my_vsnprintf.c +++ b/mysys/my_vsnprintf.c @@ -1,25 +1,25 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" #include <m_string.h> #include <stdarg.h> #include <m_ctype.h> +#include <assert.h> int my_snprintf(char* to, size_t n, const char* fmt, ...) { @@ -40,7 +40,7 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap) *to++= *fmt; /* Copy ordinary char */ continue; } - /* Skipp if max size is used (to be compatible with printf) */ + /* Skip if max size is used (to be compatible with printf) */ fmt++; while (isdigit(*fmt) || *fmt == '.' || *fmt == '-') fmt++; @@ -49,14 +49,13 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap) if (*fmt == 's') /* String parameter */ { reg2 char *par = va_arg(ap, char *); - uint plen; + uint plen,left_len = (uint)(end-to); if (!par) par = (char*)"(null)"; plen = (uint) strlen(par); - if ((uint) (end-to) > plen) /* Replace if possible */ - { - to=strmov(to,par); - continue; - } + if (left_len <= plen) + plen = left_len - 1; + to=strnmov(to,par,plen); + continue; } else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */ { @@ -75,26 +74,34 @@ int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap) break; *to++='%'; /* % used as % or unknown code */ } + DBUG_ASSERT(to <= end); *to='\0'; /* End of errmessage */ return (uint) (to - start); } #ifdef MAIN +#define OVERRUN_SENTRY 250 static void my_printf(const char * fmt, ...) { - char buf[32]; + char buf[33]; int n; va_list ar; va_start(ar, fmt); - n = my_vsnprintf(buf, sizeof(buf),fmt, ar); + buf[sizeof(buf)-1]=OVERRUN_SENTRY; + n = my_vsnprintf(buf, sizeof(buf)-1,fmt, ar); printf(buf); printf("n=%d, strlen=%d\n", n, strlen(buf)); + if (buf[sizeof(buf)-1] != OVERRUN_SENTRY) + { + fprintf(stderr, "Buffer overrun\n"); + abort(); + } va_end(ar); } int main() { - + my_printf("Hello\n"); my_printf("Hello int, %d\n", 1); my_printf("Hello string '%s'\n", "I am a string"); @@ -109,4 +116,3 @@ int main() return 0; } #endif - diff --git a/mysys/my_wincond.c b/mysys/my_wincond.c index 0c5b01f90d8..8c497e8f250 100644 --- a/mysys/my_wincond.c +++ b/mysys/my_wincond.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /***************************************************************************** ** The following is a simple implementation of posix conditions diff --git a/mysys/my_winsem.c b/mysys/my_winsem.c new file mode 100644 index 00000000000..e2713d189b2 --- /dev/null +++ b/mysys/my_winsem.c @@ -0,0 +1,406 @@ +/* + * ------------------------------------------------------------- + * + * Module: my_semaphore.c (Original: semaphore.c from pthreads library) + * + * Purpose: + * Semaphores aren't actually part of the PThreads standard. + * They are defined by the POSIX Standard: + * + * POSIX 1003.1b-1993 (POSIX.1b) + * + * ------------------------------------------------------------- + * + * Pthreads-win32 - POSIX Threads Library for Win32 + * Copyright (C) 1998 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library 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 + */ + +/* + NEED_SEM is not used in MySQL and should only be needed under + Windows CE. + + The big changes compared to the original version was to not allocate + any additional memory in sem_init() but to instead store everthing + we need in sem_t. + + TODO: + To get HAVE_CREATESEMAPHORE we have to define the struct + in my_semaphore.h +*/ + +#include "mysys_priv.h" +#ifdef __WIN__ +#include "my_semaphore.h" +#include <errno.h> + +/* + DOCPUBLIC + This function initializes an unnamed semaphore. the + initial value of the semaphore is 'value' + + PARAMETERS + sem Pointer to an instance of sem_t + + pshared If zero, this semaphore may only be shared between + threads in the same process. + If nonzero, the semaphore can be shared between + processes + + value Initial value of the semaphore counter + + RESULTS + 0 Successfully created semaphore, + -1 Failed, error in errno + + ERRNO + EINVAL 'sem' is not a valid semaphore, + ENOSPC A required resource has been exhausted, + ENOSYS Semaphores are not supported, + EPERM The process lacks appropriate privilege + +*/ + +int +sem_init (sem_t *sem, int pshared, unsigned int value) +{ + int result = 0; + + if (pshared != 0) + { + /* + We don't support creating a semaphore that can be shared between + processes + */ + result = EPERM; + } + else + { +#ifndef HAVE_CREATESEMAPHORE + sem->value = value; + sem->event = CreateEvent(NULL, + FALSE, /* manual reset */ + FALSE, /* initial state */ + NULL); + if (!sem->event) + result = ENOSPC; + else + { + if (value) + SetEvent(sem->event); + InitializeCriticalSection(&sem->sem_lock_cs); + } +#else /* HAVE_CREATESEMAPHORE */ + *sem = CreateSemaphore (NULL, /* Always NULL */ + value, /* Initial value */ + 0x7FFFFFFFL, /* Maximum value */ + NULL); /* Name */ + if (!*sem) + result = ENOSPC; +#endif /* HAVE_CREATESEMAPHORE */ + } + if (result != 0) + { + errno = result; + return -1; + } + return 0; +} /* sem_init */ + + +/* + DOCPUBLIC + This function destroys an unnamed semaphore. + + PARAMETERS + sem Pointer to an instance of sem_t + + RESULTS + 0 Successfully destroyed semaphore, + -1 Failed, error in errno + ERRNO + EINVAL 'sem' is not a valid semaphore, + ENOSYS Semaphores are not supported, + EBUSY Threads (or processes) are currently blocked on 'sem' +*/ + +int +sem_destroy (sem_t * sem) +{ + int result = 0; + +#ifdef EXTRA_DEBUG + if (sem == NULL || *sem == NULL) + { + errno=EINVAL; + return; + } +#endif /* EXTRA_DEBUG */ + +#ifndef HAVE_CREATESEMAPHORE + if (! CloseHandle(sem->event)) + result = EINVAL; + else + DeleteCriticalSection(&sem->sem_lock_cs); +#else /* HAVE_CREATESEMAPHORE */ + if (!CloseHandle(*sem)) + result = EINVAL; +#endif /* HAVE_CREATESEMAPHORE */ + if (result) + { + errno = result; + return -1; + } + *sem=0; /* Safety */ + return 0; +} /* sem_destroy */ + + +/* + DOCPUBLIC + This function tries to wait on a semaphore. If the + semaphore value is greater than zero, it decreases + its value by one. If the semaphore value is zero, then + this function returns immediately with the error EAGAIN + + PARAMETERS + sem Pointer to an instance of sem_t + + RESULTS + 0 Successfully decreased semaphore, + -1 Failed, error in errno + + ERRNO + EAGAIN The semaphore was already locked, + EINVAL 'sem' is not a valid semaphore, + ENOSYS Semaphores are not supported, + EINTR The function was interrupted by a signal, + EDEADLK A deadlock condition was detected. +*/ + +int +sem_trywait(sem_t * sem) +{ +#ifndef HAVE_CREATESEMAPHORE + /* not yet implemented! */ + int errno = EINVAL; + return -1; +#else /* HAVE_CREATESEMAPHORE */ +#ifdef EXTRA_DEBUG + if (sem == NULL || *sem == NULL) + { + errno=EINVAL; + return -1; + } +#endif /* EXTRA_DEBUG */ + if (WaitForSingleObject (*sem, 0) == WAIT_TIMEOUT) + { + errno= EAGAIN; + return -1; + } + return 0; +#endif /* HAVE_CREATESEMAPHORE */ + +} /* sem_trywait */ + + +#ifndef HAVE_CREATESEMAPHORE + +static void +ptw32_decrease_semaphore(sem_t * sem) +{ + EnterCriticalSection(&sem->sem_lock_cs); + DBUG_ASSERT(sem->value != 0); + sem->value--; + if (sem->value != 0) + SetEvent(sem->event); + LeaveCriticalSection(&sem->sem_lock_cs); +} + +static BOOL +ptw32_increase_semaphore(sem_t * sem, unsigned int n) +{ + BOOL result=FALSE; + + EnterCriticalSection(&sem->sem_lock_cs); + if (sem->value + n > sem->value) + { + sem->value += n; + SetEvent(sem->event); + result = TRUE; + } + LeaveCriticalSection(&sem->sem_lock_cs); + return result; +} + +#endif /* HAVE_CREATESEMAPHORE */ + + +/* + ------------------------------------------------------ + DOCPUBLIC + This function waits on a semaphore. If the + semaphore value is greater than zero, it decreases + its value by one. If the semaphore value is zero, then + the calling thread (or process) is blocked until it can + successfully decrease the value or until interrupted by + a signal. + + PARAMETERS + sem Pointer to an instance of sem_t + + RESULTS + 0 Successfully decreased semaphore, + -1 Failed, error in errno + + ERRNO + EINVAL 'Sem' is not a valid semaphore, + ENOSYS Semaphores are not supported, + EINTR The function was interrupted by a signal, + EDEADLK A deadlock condition was detected. +*/ + +int +sem_wait(sem_t *sem) +{ + int result; + +#ifdef EXTRA_DEBUG + if (sem == NULL || *sem == NULL) + { + errno=EINVAL; + return -1; + } +#endif /* EXTRA_DEBUG */ + +#ifndef HAVE_CREATESEMAPHORE + result=WaitForSingleObject(sem->event, INFINITE); +#else + result=WaitForSingleObject(*sem, INFINITE); +#endif + if (result == WAIT_FAILED || result == WAIT_ABANDONED_0) + result = EINVAL; + else if (result == WAIT_TIMEOUT) + result = ETIMEDOUT; + else + result=0; + if (result) + { + errno = result; + return -1; + } +#ifndef HAVE_CREATESEMAPHORE + ptw32_decrease_semaphore(sem); +#endif /* HAVE_CREATESEMAPHORE */ + return 0; +} + + +/* + ------------------------------------------------------ + DOCPUBLIC + This function posts a wakeup to a semaphore. If there + are waiting threads (or processes), one is awakened; + otherwise, the semaphore value is incremented by one. + + PARAMETERS + sem Pointer to an instance of sem_t + + RESULTS + 0 Successfully posted semaphore, + -1 Failed, error in errno + + ERRNO + EINVAL 'sem' is not a valid semaphore, + ENOSYS Semaphores are not supported, + +*/ + +int +sem_post (sem_t * sem) +{ +#ifdef EXTRA_DEBUG + if (sem == NULL || *sem == NULL) + { + errno=EINVAL; + return -1; + } +#endif /* EXTRA_DEBUG */ + +#ifndef HAVE_CREATESEMAPHORE + if (! ptw32_increase_semaphore(sem, 1)) +#else /* HAVE_CREATESEMAPHORE */ + if (! ReleaseSemaphore(*sem, 1, 0)) +#endif /* HAVE_CREATESEMAPHORE */ + { + errno=EINVAL; + return -1; + } + return 0; +} + + +/* + ------------------------------------------------------ + DOCPUBLIC + This function posts multiple wakeups to a semaphore. If there + are waiting threads (or processes), n <= count are awakened; + the semaphore value is incremented by count - n. + + PARAMETERS + sem Pointer to an instance of sem_t + count Counter, must be greater than zero. + + RESULTS + 0 Successfully posted semaphore, + -1 Failed, error in errno + + ERRNO + EINVAL 'sem' is not a valid semaphore or count is less + than or equal to zero. +*/ + +int +sem_post_multiple (sem_t * sem, unsigned int count) +{ +#ifdef EXTRA_DEBUG + if (sem == NULL || *sem == NULL || count <= 0) + { + errno=EINVAL; + return -1; + } +#endif /* EXTRA_DEBUG */ +#ifndef HAVE_CREATESEMAPHORE + if (! ptw32_increase_semaphore (sem, count)) +#else /* HAVE_CREATESEMAPHORE */ + if (! ReleaseSemaphore(*sem, count, 0)) +#endif /* HAVE_CREATESEMAPHORE */ + { + errno = EINVAL; + return -1; + } + return 0; +} + +int +sem_getvalue (sem_t *sem, unsigned int *sval) +{ + errno = ENOSYS; + return -1; +} /* sem_getvalue */ + +#endif /* __WIN__ */ diff --git a/mysys/my_winthread.c b/mysys/my_winthread.c index a77167c23e4..eebc07df180 100644 --- a/mysys/my_winthread.c +++ b/mysys/my_winthread.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /***************************************************************************** ** Simulation of posix threads calls for WIN95 and NT diff --git a/mysys/my_write.c b/mysys/my_write.c index 5d5a48a3dda..61fd6097e28 100644 --- a/mysys/my_write.c +++ b/mysys/my_write.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" #include "mysys_err.h" diff --git a/mysys/mysys_priv.h b/mysys/mysys_priv.h index 2049347cb36..f79431a0b0b 100644 --- a/mysys/mysys_priv.h +++ b/mysys/mysys_priv.h @@ -1,21 +1,20 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <global.h> +#include <my_global.h> #include <my_sys.h> #ifdef USE_SYSTEM_WRAPPERS @@ -24,9 +23,9 @@ #ifdef THREAD #include <my_pthread.h> -extern pthread_mutex_t THR_LOCK_malloc,THR_LOCK_open,THR_LOCK_keycache, - THR_LOCK_lock,THR_LOCK_isam,THR_LOCK_net,THR_LOCK_charset; -extern pthread_mutex_t LOCK_bitmap; +extern pthread_mutex_t THR_LOCK_malloc, THR_LOCK_open, THR_LOCK_keycache; +extern pthread_mutex_t THR_LOCK_lock, THR_LOCK_isam, THR_LOCK_net; +extern pthread_mutex_t THR_LOCK_charset; #else #include <my_no_pthread.h> #endif diff --git a/mysys/ptr_cmp.c b/mysys/ptr_cmp.c index 65b2c51aafd..5fc7ccab4fa 100644 --- a/mysys/ptr_cmp.c +++ b/mysys/ptr_cmp.c @@ -1,27 +1,23 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* get_ptr_compare(len) returns a pointer to a optimal byte-compare function for a array of stringpointer where all strings have size len. The bytes are compare as unsigned chars. - Because the size is saved in a static variable. - When using threads the program must have called my_init and the thread - my_init_thread() */ #include "mysys_priv.h" diff --git a/mysys/queues.c b/mysys/queues.c index 1c7a1a4a618..fe642131d74 100644 --- a/mysys/queues.c +++ b/mysys/queues.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Code for generell handling of priority Queues. @@ -124,7 +123,6 @@ byte *queue_remove(register QUEUE *queue, uint idx) } } - /* Fix when element on top has been replaced */ #ifndef queue_replaced @@ -166,3 +164,22 @@ void _downheap(register QUEUE *queue, uint idx) } queue->root[idx]=element; } + + +static int queue_fix_cmp(QUEUE *queue, void **a, void **b) +{ + return queue->compare(queue->first_cmp_arg, + (char*) (*a)+queue->offset_to_key, + (char*) (*b)+queue->offset_to_key); +} + +/* + Fix heap when every element was changed, + actually, it can be done better, in linear time, not in n*log(n) +*/ + +void queue_fix(QUEUE *queue) +{ + qsort2(queue->root+1,queue->elements, sizeof(void *), + (qsort2_cmp)queue_fix_cmp, queue); +} diff --git a/mysys/raid.cc b/mysys/raid.cc index 47326f29f60..d6359dc0f93 100644 --- a/mysys/raid.cc +++ b/mysys/raid.cc @@ -1,82 +1,81 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ - -/* --------------------------------------------------------* -* -* RAID support for MySQL. Raid 0 (stiping) only implemented yet. -* -* Why RAID? Why it must be in MySQL? -* -* This is because then you can: -* 1. Have bigger tables than your OS limit. In time of writing this -* we are hitting to 2GB limit under linux/ext2 -* 2. You can get more speed from IO bottleneck by putting -* Raid dirs on different physical disks. -* 3. Getting more fault tolerance (not implemented yet) -* -* Why not to use RAID: -* -* 1. You are losing some processor power to calculate things, -* do more syscalls and interrupts. -* -* Functionality is supplied by two classes: RaidFd and RaidName. -* RaidFd supports funtionality over file descriptors like -* open/create/write/seek/close. RaidName supports functionality -* like rename/delete where we have no relations to filedescriptors. -* RaidName can be prorably unchanged for different Raid levels. RaidFd -* have to be virtual I think ;). -* You can speed up some calls in MySQL code by skipping RAID code. -* For example LOAD DATA INFILE never needs to read RAID-ed files. -* This can be done adding proper "#undef my_read" or similar undef-s -* in your code. Check out the raid.h! -* -* Some explanation about _seek_vector[] -* This is seek cache. RAID seeks too much and we cacheing this. We -* fool it and just storing new position in file to _seek_vector. -* When there is no seeks to do, we are putting RAID_SEEK_DONE into it. -* Any other value requires seeking to that position. -* -* TODO: -* -* -* - Implement other fancy things like RAID 1 (mirroring) and RAID 5. -* Should not to be very complex. -* -* - Optimize big blob writes by resorting write buffers and writing -* big chunks at once instead of doing many syscalls. - after thinking I -* found this is useless. This is because same thing one can do with just -* increasing RAID_CHUNKSIZE. Monty, what do you think? tonu. -* -* - If needed, then implement missing syscalls. One known to miss is stat(); -* -* - Make and use a thread safe dynamic_array buffer. The used one -* will not work if needs to be extended at the same time someone is -* accessing it. -* -* -* tonu@mysql.com & monty@mysql.com -* --------------------------------------------------------*/ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + + RAID support for MySQL. Raid 0 (stiping) only implemented yet. + + Why RAID? Why it must be in MySQL? + + This is because then you can: + 1. Have bigger tables than your OS limit. In time of writing this + we are hitting to 2GB limit under linux/ext2 + 2. You can get more speed from IO bottleneck by putting + Raid dirs on different physical disks. + 3. Getting more fault tolerance (not implemented yet) + + Why not to use RAID: + + 1. You are losing some processor power to calculate things, + do more syscalls and interrupts. + + Functionality is supplied by two classes: RaidFd and RaidName. + RaidFd supports funtionality over file descriptors like + open/create/write/seek/close. RaidName supports functionality + like rename/delete where we have no relations to filedescriptors. + RaidName can be prorably unchanged for different Raid levels. RaidFd + have to be virtual I think ;). + You can speed up some calls in MySQL code by skipping RAID code. + For example LOAD DATA INFILE never needs to read RAID-ed files. + This can be done adding proper "#undef my_read" or similar undef-s + in your code. Check out the raid.h! + + Some explanation about _seek_vector[] + This is seek cache. RAID seeks too much and we cacheing this. We + fool it and just storing new position in file to _seek_vector. + When there is no seeks to do, we are putting RAID_SEEK_DONE into it. + Any other value requires seeking to that position. + + TODO: + + + - Implement other fancy things like RAID 1 (mirroring) and RAID 5. + Should not to be very complex. + + - Optimize big blob writes by resorting write buffers and writing + big chunks at once instead of doing many syscalls. - after thinking I + found this is useless. This is because same thing one can do with just + increasing RAID_CHUNKSIZE. Monty, what do you think? tonu. + + - If needed, then implement missing syscalls. One known to miss is stat(); + + - Make and use a thread safe dynamic_array buffer. The used one + will not work if needs to be extended at the same time someone is + accessing it. + + + tonu@mysql.com & monty@mysql.com +*/ #ifdef __GNUC__ #pragma implementation // gcc: Class implementation #endif #include "mysys_priv.h" -#include "my_dir.h" +#include <my_dir.h> #include <m_string.h> #include <assert.h> @@ -282,7 +281,7 @@ extern "C" { DBUG_RETURN(my_close(fd, MyFlags)); } - int my_raid_chsize(File fd, my_off_t newlength, myf MyFlags) + int my_raid_chsize(File fd, my_off_t newlength, int filler, myf MyFlags) { DBUG_ENTER("my_raid_chsize"); DBUG_PRINT("enter",("Fd: %d newlength: %u MyFlags: %d", @@ -290,10 +289,10 @@ extern "C" { if (is_raid(fd)) { RaidFd *raid= (*dynamic_element(&RaidFd::_raid_map,fd,RaidFd**)); - DBUG_RETURN(raid->Chsize(fd, newlength, MyFlags)); + DBUG_RETURN(raid->Chsize(fd, newlength, filler, MyFlags)); } else - DBUG_RETURN(my_chsize(fd, newlength, MyFlags)); + DBUG_RETURN(my_chsize(fd, newlength, filler, MyFlags)); } int my_raid_rename(const char *from, const char *to, @@ -410,8 +409,8 @@ IsRaid(File fd) RaidFd:: RaidFd(uint raid_type, uint raid_chunks, ulong raid_chunksize) :_raid_type(raid_type), _raid_chunks(raid_chunks), - _raid_chunksize(raid_chunksize), _position(0), _fd_vector(0), - _size(RAID_SIZE_UNKNOWN) + _raid_chunksize(raid_chunksize), _position(0), _size(RAID_SIZE_UNKNOWN), + _fd_vector(0) { DBUG_ENTER("RaidFd::RaidFd"); DBUG_PRINT("enter",("RaidFd_type: %u Disks: %u Chunksize: %d", @@ -739,7 +738,7 @@ Tell(myf MyFlags) } int RaidFd:: -Chsize(File fd, my_off_t newlength, myf MyFlags) +Chsize(File fd, my_off_t newlength, int filler, myf MyFlags) { DBUG_ENTER("RaidFd::Chsize"); DBUG_PRINT("enter",("Fd: %d, newlength: %d, MyFlags: %d", @@ -753,17 +752,16 @@ Chsize(File fd, my_off_t newlength, myf MyFlags) if ( i < _this_block ) newpos = my_chsize(_fd_vector[i], _this_block * _raid_chunksize + (_rounds + 1) * - _raid_chunksize, - MyFlags); + _raid_chunksize, filler, MyFlags); else if ( i == _this_block ) newpos = my_chsize(_fd_vector[i], _this_block * _raid_chunksize + _rounds * _raid_chunksize + (newlength % _raid_chunksize), - MyFlags); + filler, MyFlags); else // this means: i > _this_block newpos = my_chsize(_fd_vector[i], _this_block * _raid_chunksize + _rounds * - _raid_chunksize, MyFlags); + _raid_chunksize, filler, MyFlags); if (newpos) DBUG_RETURN(1); } diff --git a/mysys/rijndael.c b/mysys/rijndael.c new file mode 100644 index 00000000000..dd0c45445d5 --- /dev/null +++ b/mysys/rijndael.c @@ -0,0 +1,1397 @@ +/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + + +/* + Based on version 3.0 (December 2000) + + Optimised ANSI C code for the Rijndael cipher (now AES) + + author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be> + author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be> + author Paulo Barreto <paulo.barreto@terra.com.br> +*/ + +#include <my_global.h> +#include <assert.h> +#include "rijndael.h" + +/* + Define the following to use fastest and much larger code (~10K extra code) + #define FULL_UNROLL +*/ + + +#ifdef NOT_USED +Te0[x] = S [x].[02, 01, 01, 03]; +Te1[x] = S [x].[03, 02, 01, 01]; +Te2[x] = S [x].[01, 03, 02, 01]; +Te3[x] = S [x].[01, 01, 03, 02]; +Te4[x] = S [x].[01, 01, 01, 01]; + +Td0[x] = Si[x].[0e, 09, 0d, 0b]; +Td1[x] = Si[x].[0b, 0e, 09, 0d]; +Td2[x] = Si[x].[0d, 0b, 0e, 09]; +Td3[x] = Si[x].[09, 0d, 0b, 0e]; +Td4[x] = Si[x].[01, 01, 01, 01]; +#endif + + +static const uint32 Te0[256]= +{ + 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, + 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, + 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, + 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, + 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, + 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, + 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, + 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, + 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, + 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, + 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, + 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, + 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, + 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, + 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, + 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, + 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, + 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, + 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, + 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, + 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, + 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, + 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, + 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, + 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, + 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, + 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, + 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, + 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, + 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, + 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, + 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, + 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, + 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, + 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, + 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, + 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, + 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, + 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, + 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, + 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, + 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, + 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, + 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, + 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, + 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, + 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, + 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, + 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, + 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, + 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, + 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, + 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, + 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, + 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, + 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, + 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, + 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, + 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, + 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, + 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, + 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, + 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, + 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, +}; + +static const uint32 Te1[256]= +{ + 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, + 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, + 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, + 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, + 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, + 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, + 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, + 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, + 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, + 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, + 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, + 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, + 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, + 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, + 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, + 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, + 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, + 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, + 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, + 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, + 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, + 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, + 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, + 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, + 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, + 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, + 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, + 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, + 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, + 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, + 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, + 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, + 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, + 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, + 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, + 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, + 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, + 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, + 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, + 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, + 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, + 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, + 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, + 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, + 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, + 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, + 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, + 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, + 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, + 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, + 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, + 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, + 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, + 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, + 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, + 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, + 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, + 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, + 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, + 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, + 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, + 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, + 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, + 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, +}; + +static const uint32 Te2[256]= +{ + 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, + 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, + 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, + 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, + 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, + 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, + 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, + 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, + 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, + 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, + 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, + 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, + 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, + 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, + 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, + 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, + 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, + 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, + 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, + 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, + 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, + 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, + 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, + 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, + 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, + 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, + 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, + 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, + 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, + 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, + 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, + 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, + 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, + 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, + 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, + 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, + 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, + 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, + 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, + 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, + 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, + 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, + 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, + 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, + 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, + 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, + 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, + 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, + 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, + 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, + 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, + 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, + 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, + 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, + 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, + 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, + 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, + 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, + 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, + 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, + 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, + 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, + 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, + 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, +}; + +static const uint32 Te3[256]= +{ + 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, + 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, + 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, + 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, + 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, + 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, + 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, + 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, + 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, + 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, + 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, + 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, + 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, + 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, + 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, + 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, + 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, + 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, + 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, + 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, + 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, + 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, + 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, + 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, + 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, + 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, + 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, + 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, + 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, + 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, + 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, + 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, + 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, + 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, + 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, + 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, + 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, + 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, + 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, + 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, + 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, + 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, + 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, + 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, + 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, + 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, + 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, + 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, + 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, + 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, + 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, + 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, + 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, + 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, + 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, + 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, + 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, + 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, + 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, + 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, + 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, + 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, + 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, + 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, +}; + +static const uint32 Te4[256]= +{ + 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, + 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, + 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, + 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, + 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, + 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, + 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, + 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, + 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, + 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, + 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, + 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, + 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, + 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, + 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, + 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, + 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, + 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, + 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, + 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, + 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, + 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, + 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, + 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, + 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, + 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, + 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, + 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, + 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, + 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, + 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, + 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, + 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, + 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, + 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, + 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, + 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, + 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, + 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, + 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, + 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, + 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, + 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, + 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, + 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, + 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, + 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, + 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, + 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, + 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, + 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, + 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, + 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, + 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, + 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, + 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, + 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, + 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, + 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, + 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, + 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, + 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, + 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, + 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, +}; + +static const uint32 Td0[256]= +{ + 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, + 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, + 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, + 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, + 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, + 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, + 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, + 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, + 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, + 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, + 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, + 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, + 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, + 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, + 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, + 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, + 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, + 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, + 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, + 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, + 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, + 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, + 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, + 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, + 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, + 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, + 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, + 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, + 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, + 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, + 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, + 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, + 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, + 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, + 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, + 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, + 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, + 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, + 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, + 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, + 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, + 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, + 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, + 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, + 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, + 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, + 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, + 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, + 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, + 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, + 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, + 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, + 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, + 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, + 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, + 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, + 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, + 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, + 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, + 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, + 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, + 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, + 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, + 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, +}; + +static const uint32 Td1[256]= +{ + 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, + 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, + 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, + 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, + 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, + 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, + 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, + 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, + 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, + 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, + 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, + 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, + 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, + 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, + 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, + 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, + 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, + 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, + 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, + 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, + 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, + 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, + 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, + 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, + 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, + 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, + 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, + 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, + 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, + 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, + 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, + 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, + 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, + 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, + 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, + 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, + 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, + 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, + 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, + 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, + 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, + 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, + 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, + 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, + 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, + 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, + 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, + 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, + 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, + 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, + 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, + 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, + 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, + 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, + 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, + 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, + 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, + 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, + 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, + 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, + 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, + 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, + 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, + 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, +}; + +static const uint32 Td2[256]= +{ + 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, + 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, + 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, + 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, + 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, + 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, + 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, + 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, + 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, + 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, + 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, + 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, + 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, + 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, + 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, + 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, + 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, + 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, + 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, + 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, + + 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, + 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, + 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, + 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, + 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, + 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, + 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, + 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, + 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, + 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, + 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, + 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, + 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, + 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, + 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, + 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, + 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, + 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, + 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, + 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, + 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, + 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, + 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, + 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, + 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, + 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, + 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, + 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, + 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, + 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, + 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, + 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, + 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, + 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, + 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, + 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, + 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, + 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, + 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, + 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, + 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, + 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, + 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, + 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, +}; + +static const uint32 Td3[256]= +{ + 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, + 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, + 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, + 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, + 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, + 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, + 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, + 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, + 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, + 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, + 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, + 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, + 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, + 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, + 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, + 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, + 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, + 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, + 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, + 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, + 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, + 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, + 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, + 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, + 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, + 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, + 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, + 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, + 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, + 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, + 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, + 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, + 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, + 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, + 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, + 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, + 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, + 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, + 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, + 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, + 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, + 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, + 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, + 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, + 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, + 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, + 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, + 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, + 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, + 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, + 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, + 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, + 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, + 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, + 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, + 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, + 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, + 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, + 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, + 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, + 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, + 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, + 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, + 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, +}; + +static const uint32 Td4[256]= +{ + 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, + 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, + 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, + 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, + 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, + 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, + 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, + 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, + 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, + 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, + 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, + 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, + 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, + 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, + 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, + 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, + 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, + 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, + 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, + 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, + 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, + 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, + 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, + 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, + 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, + 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, + 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, + 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, + 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, + 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, + 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, + 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, + 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, + 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, + 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, + 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, + 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, + 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, + 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, + 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, + 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, + 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, + 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, + 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, + 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, + 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, + 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, + 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, + 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, + 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, + 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, + 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, + 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, + 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, + 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, + 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, + 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, + 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, + 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, + 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, + 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, + 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, + 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, + 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, +}; + + +/* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ +static const uint32 rcon[]= +{ + 0x01000000, 0x02000000, 0x04000000, 0x08000000, + 0x10000000, 0x20000000, 0x40000000, 0x80000000, + 0x1B000000, 0x36000000, +}; + +#if defined(_MSC_VER) && defined(__i386__) + +#define RJ_SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) +#define GETuint32(p) RJ_SWAP(*((uint32 *)(p))) +#define PUTuint32(ct, st) { *((uint32 *)(ct)) = RJ_SWAP((st)); } + +#else + +#define GETuint32(pt) (((uint32)(pt)[0] << 24) ^ ((uint32)(pt)[1] << 16)\ + ^ ((uint32)(pt)[2] << 8) ^ ((uint32)(pt)[3])) +#define PUTuint32(ct, st) { (ct)[0] = (uint8)((st) >> 24); (ct)[1]\ += (uint8)((st) >> 16); (ct)[2] = (uint8)((st) >> 8); (ct)[3] = (uint8)(st); } + +#endif /* defined(_MSC_VER) && defined(__i386__) */ + + +/* + Expand the cipher key into the encryption key schedule. + + RETURN + The number of rounds for the given cipher key size. +*/ + +int rijndaelKeySetupEnc(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[], + int keyBits) +{ + int i = 0; + uint32 temp; + + rk[0] = GETuint32(cipherKey ); + rk[1] = GETuint32(cipherKey + 4); + rk[2] = GETuint32(cipherKey + 8); + rk[3] = GETuint32(cipherKey + 12); + if (keyBits == 128) + { + for (;;) + { + temp = rk[3]; + rk[4] = (rk[0] ^ + (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (Te4[(temp ) & 0xff] & 0x0000ff00) ^ + (Te4[(temp >> 24) ] & 0x000000ff) ^ + rcon[i]); + rk[5] = rk[1] ^ rk[4]; + rk[6] = rk[2] ^ rk[5]; + rk[7] = rk[3] ^ rk[6]; + if (++i == 10) + return 10; + rk += 4; + } + } + rk[4] = GETuint32(cipherKey + 16); + rk[5] = GETuint32(cipherKey + 20); + if (keyBits == 192) + { + for (;;) + { + temp = rk[ 5]; + rk[ 6] = (rk[ 0] ^ + (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (Te4[(temp ) & 0xff] & 0x0000ff00) ^ + (Te4[(temp >> 24) ] & 0x000000ff) ^ + rcon[i]); + rk[ 7] = rk[ 1] ^ rk[ 6]; + rk[ 8] = rk[ 2] ^ rk[ 7]; + rk[ 9] = rk[ 3] ^ rk[ 8]; + if (++i == 8) + { + return 12; + } + rk[10] = rk[ 4] ^ rk[ 9]; + rk[11] = rk[ 5] ^ rk[10]; + rk += 6; + } + } + rk[6] = GETuint32(cipherKey + 24); + rk[7] = GETuint32(cipherKey + 28); + if (keyBits == 256) + { + for (;;) + { + temp = rk[ 7]; + rk[ 8] = (rk[ 0] ^ + (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (Te4[(temp ) & 0xff] & 0x0000ff00) ^ + (Te4[(temp >> 24) ] & 0x000000ff) ^ + rcon[i]); + rk[ 9] = rk[ 1] ^ rk[ 8]; + rk[10] = rk[ 2] ^ rk[ 9]; + rk[11] = rk[ 3] ^ rk[10]; + if (++i == 7) + { + return 14; + } + temp = rk[11]; + rk[12] = (rk[ 4] ^ + (Te4[(temp >> 24) ] & 0xff000000) ^ + (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(temp ) & 0xff] & 0x000000ff)); + rk[13] = rk[ 5] ^ rk[12]; + rk[14] = rk[ 6] ^ rk[13]; + rk[15] = rk[ 7] ^ rk[14]; + rk += 8; + } + } + return 0; +} + + +/* + Expand the cipher key into the decryption key schedule. + + RETURN + The number of rounds for the given cipher key size. +*/ + +int rijndaelKeySetupDec(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[], + int keyBits) +{ + int nr, i, j; + uint32 temp; + + /* expand the cipher key: */ + nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits); + /* invert the order of the round keys: */ + for (i = 0, j = 4*nr; i < j; i += 4, j -= 4) + { + temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; + temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; + temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; + temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; + } + /* + Apply the inverse MixColumn transform to all round keys but the first + and the last: + */ + for (i = 1; i < nr; i++) + { + rk += 4; + + rk[0]= ( + Td0[Te4[(rk[0] >> 24) ] & 0xff] ^ + Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^ + Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^ + Td3[Te4[(rk[0] ) & 0xff] & 0xff]); + + rk[1]= (Td0[Te4[(rk[1] >> 24) ] & 0xff] ^ + Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^ + Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^ + Td3[Te4[(rk[1] ) & 0xff] & 0xff]); + + rk[2]= (Td0[Te4[(rk[2] >> 24) ] & 0xff] ^ + Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^ + Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^ + Td3[Te4[(rk[2] ) & 0xff] & 0xff]); + + rk[3]= (Td0[Te4[(rk[3] >> 24) ] & 0xff] ^ + Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^ + Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^ + Td3[Te4[(rk[3] ) & 0xff] & 0xff]); + } + return nr; +} + + +void rijndaelEncrypt(const uint32 rk[/*4*(Nr + 1)*/], int Nr, + const uint8 pt[16], uint8 ct[16]) +{ + uint32 s0, s1, s2, s3, t0, t1, t2, t3; +#ifndef FULL_UNROLL + int r; +#endif /* FULL_UNROLL */ + + /* map byte array block to cipher state and add initial round key: */ + s0 = GETuint32(pt ) ^ rk[0]; + s1 = GETuint32(pt + 4) ^ rk[1]; + s2 = GETuint32(pt + 8) ^ rk[2]; + s3 = GETuint32(pt + 12) ^ rk[3]; + +#ifdef FULL_UNROLL + /* round 1: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[ 4]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[ 5]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[ 6]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[ 7]); + + /* round 2: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[ 8]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[ 9]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[10]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[11]); + + /* round 3: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[12]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[13]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[14]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[15]); + + /* round 4: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[16]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[17]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[18]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[19]); + + /* round 5: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[20]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[21]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[22]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[23]); + + /* round 6: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[24]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[25]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[26]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[27]); + + /* round 7: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[28]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[29]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[30]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[31]); + + /* round 8: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[32]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[33]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[34]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[35]); + + /* round 9: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[36]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[37]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[38]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[39]); + + if (Nr > 10) + { + /* round 10: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[40]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[41]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[42]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[43]); + + /* round 11: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[44]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[45]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[46]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[47]); + + if (Nr > 12) + { + /* round 12: */ + s0= (Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] + ^ Te3[t3 & 0xff] ^ rk[48]); + s1= (Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] + ^ Te3[t0 & 0xff] ^ rk[49]); + s2= (Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] + ^ Te3[t1 & 0xff] ^ rk[50]); + s3= (Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] + ^ Te3[t2 & 0xff] ^ rk[51]); + + /* round 13: */ + t0= (Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] + ^ Te3[s3 & 0xff] ^ rk[52]); + t1= (Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] + ^ Te3[s0 & 0xff] ^ rk[53]); + t2= (Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] + ^ Te3[s1 & 0xff] ^ rk[54]); + t3= (Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] + ^ Te3[s2 & 0xff] ^ rk[55]); + } + } + rk += Nr << 2; +#else /* !FULL_UNROLL */ + + /* Nr - 1 full rounds: */ + + r = Nr >> 1; + for (;;) + { + t0= (Te0[(s0 >> 24) ] ^ + Te1[(s1 >> 16) & 0xff] ^ + Te2[(s2 >> 8) & 0xff] ^ + Te3[(s3 ) & 0xff] ^ + rk[4]); + + t1= (Te0[(s1 >> 24) ] ^ + Te1[(s2 >> 16) & 0xff] ^ + Te2[(s3 >> 8) & 0xff] ^ + Te3[(s0 ) & 0xff] ^ + rk[5]); + + t2= (Te0[(s2 >> 24) ] ^ + Te1[(s3 >> 16) & 0xff] ^ + Te2[(s0 >> 8) & 0xff] ^ + Te3[(s1 ) & 0xff] ^ + rk[6]); + + t3= (Te0[(s3 >> 24) ] ^ + Te1[(s0 >> 16) & 0xff] ^ + Te2[(s1 >> 8) & 0xff] ^ + Te3[(s2 ) & 0xff] ^ + rk[7]); + + rk+= 8; + if (--r == 0) + break; + + s0= (Te0[(t0 >> 24) ] ^ + Te1[(t1 >> 16) & 0xff] ^ + Te2[(t2 >> 8) & 0xff] ^ + Te3[(t3 ) & 0xff] ^ + rk[0]); + + s1= (Te0[(t1 >> 24) ] ^ + Te1[(t2 >> 16) & 0xff] ^ + Te2[(t3 >> 8) & 0xff] ^ + Te3[(t0 ) & 0xff] ^ + rk[1]); + + s2= (Te0[(t2 >> 24) ] ^ + Te1[(t3 >> 16) & 0xff] ^ + Te2[(t0 >> 8) & 0xff] ^ + Te3[(t1 ) & 0xff] ^ + rk[2]); + + s3= (Te0[(t3 >> 24) ] ^ + Te1[(t0 >> 16) & 0xff] ^ + Te2[(t1 >> 8) & 0xff] ^ + Te3[(t2 ) & 0xff] ^ + rk[3]); + } +#endif /* FULL_UNROLL */ + + /* Apply last round and map cipher state to byte array block: */ + s0= ((Te4[(t0 >> 24) ] & 0xff000000) ^ + (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t3 ) & 0xff] & 0x000000ff) ^ + rk[0]); + PUTuint32(ct , s0); + + s1= ((Te4[(t1 >> 24) ] & 0xff000000) ^ + (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t0 ) & 0xff] & 0x000000ff) ^ + rk[1]); + PUTuint32(ct + 4, s1); + + s2= ((Te4[(t2 >> 24) ] & 0xff000000) ^ + (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t1 ) & 0xff] & 0x000000ff) ^ + rk[2]); + PUTuint32(ct + 8, s2); + + s3= ((Te4[(t3 >> 24) ] & 0xff000000) ^ + (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ + (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ + (Te4[(t2 ) & 0xff] & 0x000000ff) ^ + rk[3]); + PUTuint32(ct + 12, s3); +} + + +void rijndaelDecrypt(const uint32 rk[/*4*(Nr + 1)*/], int Nr, + const uint8 ct[16], uint8 pt[16]) +{ + uint32 s0, s1, s2, s3, t0, t1, t2, t3; +#ifndef FULL_UNROLL + int r; +#endif /* FULL_UNROLL */ + + /* Map byte array block to cipher state and add initial round key: */ + + s0 = GETuint32(ct ) ^ rk[0]; + s1 = GETuint32(ct + 4) ^ rk[1]; + s2 = GETuint32(ct + 8) ^ rk[2]; + s3 = GETuint32(ct + 12) ^ rk[3]; + +#ifdef FULL_UNROLL + /* round 1: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[ 4]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[ 5]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[ 6]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[ 7]); + + /* round 2: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[ 8]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[ 9]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[10]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[11]); + + /* round 3: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[12]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[13]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[14]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[15]); + + /* round 4: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[16]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[17]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[18]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[19]); + + /* round 5: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[20]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[21]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[22]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[23]); + + /* round 6: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[24]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[25]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[26]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[27]); + + /* round 7: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[28]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[29]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[30]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[31]); + + /* round 8: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[32]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[33]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[34]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[35]); + + /* round 9: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[36]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[37]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[38]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[39]); + + if (Nr > 10) + { + /* round 10: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[40]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[41]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[42]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[43]); + + /* round 11: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[44]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[45]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[46]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[47]); + + if (Nr > 12) + { + /* round 12: */ + s0= (Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] + ^ Td3[t1 & 0xff] ^ rk[48]); + s1= (Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] + ^ Td3[t2 & 0xff] ^ rk[49]); + s2= (Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] + ^ Td3[t3 & 0xff] ^ rk[50]); + s3= (Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] + ^ Td3[t0 & 0xff] ^ rk[51]); + + /* round 13: */ + t0= (Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] + ^ Td3[s1 & 0xff] ^ rk[52]); + t1= (Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] + ^ Td3[s2 & 0xff] ^ rk[53]); + t2= (Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] + ^ Td3[s3 & 0xff] ^ rk[54]); + t3= (Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] + ^ Td3[s0 & 0xff] ^ rk[55]); + } + } + rk += Nr << 2; +#else /* !FULL_UNROLL */ + + /* Nr - 1 full rounds: */ + r= (Nr >> 1); + for (;;) + { + t0= (Td0[(s0 >> 24) ] ^ + Td1[(s3 >> 16) & 0xff] ^ + Td2[(s2 >> 8) & 0xff] ^ + Td3[(s1 ) & 0xff] ^ + rk[4]); + + t1= (Td0[(s1 >> 24) ] ^ + Td1[(s0 >> 16) & 0xff] ^ + Td2[(s3 >> 8) & 0xff] ^ + Td3[(s2 ) & 0xff] ^ + rk[5]); + + t2= (Td0[(s2 >> 24) ] ^ + Td1[(s1 >> 16) & 0xff] ^ + Td2[(s0 >> 8) & 0xff] ^ + Td3[(s3 ) & 0xff] ^ + rk[6]); + + t3= (Td0[(s3 >> 24) ] ^ + Td1[(s2 >> 16) & 0xff] ^ + Td2[(s1 >> 8) & 0xff] ^ + Td3[(s0 ) & 0xff] ^ + rk[7]); + + rk+= 8; + if (--r == 0) + break; + + s0= (Td0[(t0 >> 24) ] ^ + Td1[(t3 >> 16) & 0xff] ^ + Td2[(t2 >> 8) & 0xff] ^ + Td3[(t1 ) & 0xff] ^ + rk[0]); + + s1= (Td0[(t1 >> 24) ] ^ + Td1[(t0 >> 16) & 0xff] ^ + Td2[(t3 >> 8) & 0xff] ^ + Td3[(t2 ) & 0xff] ^ + rk[1]); + + s2= (Td0[(t2 >> 24) ] ^ + Td1[(t1 >> 16) & 0xff] ^ + Td2[(t0 >> 8) & 0xff] ^ + Td3[(t3 ) & 0xff] ^ + rk[2]); + + s3= (Td0[(t3 >> 24) ] ^ + Td1[(t2 >> 16) & 0xff] ^ + Td2[(t1 >> 8) & 0xff] ^ + Td3[(t0 ) & 0xff] ^ + rk[3]); + } + +#endif /* FULL_UNROLL */ + + /* Apply last round and map cipher state to byte array block: */ + + s0= ((Td4[(t0 >> 24) ] & 0xff000000) ^ + (Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ + (Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ + (Td4[(t1 ) & 0xff] & 0x000000ff) ^ + rk[0]); + PUTuint32(pt , s0); + + s1= ((Td4[(t1 >> 24) ] & 0xff000000) ^ + (Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ + (Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ + (Td4[(t2 ) & 0xff] & 0x000000ff) ^ + rk[1]); + PUTuint32(pt + 4, s1); + + s2= ((Td4[(t2 >> 24) ] & 0xff000000) ^ + (Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ + (Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ + (Td4[(t3 ) & 0xff] & 0x000000ff) ^ + rk[2]); + PUTuint32(pt + 8, s2); + + s3= ((Td4[(t3 >> 24) ] & 0xff000000) ^ + (Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ + (Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ + (Td4[(t0 ) & 0xff] & 0x000000ff) ^ + rk[3]); + PUTuint32(pt + 12, s3); +} diff --git a/mysys/safemalloc.c b/mysys/safemalloc.c index 0bf9341e3c1..9cbb178edb4 100644 --- a/mysys/safemalloc.c +++ b/mysys/safemalloc.c @@ -1,68 +1,64 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ +/* Copyright (C) 2000 MySQL AB -/* - * [This posting refers to an article entitled "oops, corrupted memory - * again!" in net.lang.c. I am posting it here because it is source.] - * - * My tool for approaching this problem is to build another level of data - * abstraction on top of malloc() and free() that implements some checking. - * This does a number of things for you: - * - Checks for overruns and underruns on allocated data - * - Keeps track of where in the program the memory was malloc'ed - * - Reports on pieces of memory that were not free'ed - * - Records some statistics such as maximum memory used - * - Marks newly malloc'ed and newly free'ed memory with special values - * You can use this scheme to: - * - Find bugs such as overrun, underrun, etc because you know where - * a piece of data was malloc'ed and where it was free'ed - * - Find bugs where memory was not free'ed - * - Find bugs where newly malloc'ed memory is used without initializing - * - Find bugs where newly free'ed memory is still used - * - Determine how much memory your program really uses - * - and other things - */ + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. -/* - * To implement my scheme you must have a C compiler that has __LINE__ and - * __FILE__ macros. If your compiler doesn't have these then (a) buy another: - * compilers that do are available on UNIX 4.2bsd based systems and the PC, - * and probably on other machines; or (b) change my scheme somehow. I have - * recomendations on both these points if you would like them (e-mail please). - * - * There are 4 functions in my package: - * char *NEW( uSize ) Allocate memory of uSize bytes - * (equivalent to malloc()) - * char *REA( pPtr, uSize) Allocate memory of uSize bytes, move data and - * free pPtr. - * (equivalent to realloc()) - * FREE( pPtr ) Free memory allocated by NEW - * (equivalent to free()) - * TERMINATE(file) End system, report errors and stats on file - * I personally use two more functions, but have not included them here: - * char *STRSAVE( sPtr ) Save a copy of the string in dynamic memory - * char *RENEW( pPtr, uSize ) - * (equivalent to realloc()) - */ + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * Memory sub-system, written by Bjorn Benson Fixed to use my_sys scheme by Michael Widenius - */ + + [This posting refers to an article entitled "oops, corrupted memory + again!" in net.lang.c. I am posting it here because it is source.] + + My tool for approaching this problem is to build another level of data + abstraction on top of malloc() and free() that implements some checking. + This does a number of things for you: + - Checks for overruns and underruns on allocated data + - Keeps track of where in the program the memory was malloc'ed + - Reports on pieces of memory that were not free'ed + - Records some statistics such as maximum memory used + - Marks newly malloc'ed and newly free'ed memory with special values + You can use this scheme to: + - Find bugs such as overrun, underrun, etc because you know where + a piece of data was malloc'ed and where it was free'ed + - Find bugs where memory was not free'ed + - Find bugs where newly malloc'ed memory is used without initializing + - Find bugs where newly free'ed memory is still used + - Determine how much memory your program really uses + - and other things + + To implement my scheme you must have a C compiler that has __LINE__ and + __FILE__ macros. If your compiler doesn't have these then (a) buy another: + compilers that do are available on UNIX 4.2bsd based systems and the PC, + and probably on other machines; or (b) change my scheme somehow. I have + recomendations on both these points if you would like them (e-mail please). + + There are 4 functions in my package: + char *NEW( uSize ) Allocate memory of uSize bytes + (equivalent to malloc()) + char *REA( pPtr, uSize) Allocate memory of uSize bytes, move data and + free pPtr. + (equivalent to realloc()) + FREE( pPtr ) Free memory allocated by NEW + (equivalent to free()) + TERMINATE(file) End system, report errors and stats on file + I personally use two more functions, but have not included them here: + char *STRSAVE( sPtr ) Save a copy of the string in dynamic memory + char *RENEW( pPtr, uSize ) + (equivalent to realloc()) + +*/ #ifndef SAFEMALLOC #define SAFEMALLOC /* Get protos from my_sys */ @@ -82,6 +78,16 @@ ulonglong safemalloc_mem_limit = ~(ulonglong)0; #define uDataSize tInt._uDataSize #define lSpecialValue tInt._lSpecialValue +#ifndef PEDANTIC_SAFEMALLOC +/* + Set to 1 after TERMINATE() if we had to fiddle with cNewCount and + the linked list of blocks so that _sanity() will not fuss when it + is not supposed to +*/ +static int sf_malloc_tampered = 0; +#endif + + /* Static functions prototypes */ static int check_ptr(const char *where, byte *ptr, const char *sFile, @@ -89,37 +95,37 @@ static int check_ptr(const char *where, byte *ptr, const char *sFile, static int _checkchunk(struct remember *pRec, const char *sFile, uint uLine); /* - * Note: both these refer to the NEW'ed - * data only. They do not include - * malloc() roundoff or the extra - * space required by the remember - * structures. - */ - -#define ALLOC_VAL (uchar) 0xA5 /* NEW'ed memory is filled with this */ - /* value so that references to it will */ - /* end up being very strange. */ -#define FREE_VAL (uchar) 0x8F /* FREE'ed memory is filled with this */ - /* value so that references to it will */ - /* also end up being strange. */ + Note: both these refer to the NEW'ed data only. They do not include + malloc() roundoff or the extra space required by the remember + structures. +*/ +/* + NEW'ed memory is filled with this value so that references to it will + end up being very strange. +*/ +#define ALLOC_VAL (uchar) 0xA5 +/* + FEEE'ed memory is filled with this value so that references to it will + end up being very strange. +*/ +#define FREE_VAL (uchar) 0x8F #define MAGICKEY 0x14235296 /* A magic value for underrun key */ + +/* + Warning: do not change the MAGICEND? values to something with the + high bit set. Various C compilers (like the 4.2bsd one) do not do + the sign extension right later on in this code and you will get + erroneous errors. +*/ + #define MAGICEND0 0x68 /* Magic values for overrun keys */ #define MAGICEND1 0x34 /* " */ #define MAGICEND2 0x7A /* " */ #define MAGICEND3 0x15 /* " */ - /* Warning: do not change the MAGICEND? values to */ - /* something with the high bit set. Various C */ - /* compilers (like the 4.2bsd one) do not do the */ - /* sign extension right later on in this code and */ - /* you will get erroneous errors. */ - -/* - * gptr _mymalloc( uint uSize, my_string sFile, uint uLine, MyFlags ) - * Allocate some memory. - */ +/* Allocate some memory. */ gptr _mymalloc (uint uSize, const char *sFile, uint uLine, myf MyFlags) { @@ -131,18 +137,19 @@ gptr _mymalloc (uint uSize, const char *sFile, uint uLine, myf MyFlags) if (!sf_malloc_quick) (void) _sanity (sFile, uLine); - if(uSize + lCurMemory > safemalloc_mem_limit) + if (uSize + lCurMemory > safemalloc_mem_limit) pTmp = 0; else + { /* Allocate the physical memory */ pTmp = (struct remember *) malloc ( - sizeof (struct irem) /* remember data */ + ALIGN_SIZE(sizeof(struct irem)) /* remember data */ + sf_malloc_prehunc + uSize /* size requested */ + 4 /* overrun mark */ + sf_malloc_endhunc ); - + } /* Check if there isn't anymore memory avaiable */ if (pTmp == NULL) { @@ -207,9 +214,9 @@ gptr _mymalloc (uint uSize, const char *sFile, uint uLine, myf MyFlags) } /* - * Allocate some new memory and move old memoryblock there. - * Free then old memoryblock - */ + Allocate some new memory and move old memoryblock there. + Free then old memoryblock +*/ gptr _myrealloc (register gptr pPtr, register uint uSize, const char *sFile, uint uLine, myf MyFlags) @@ -227,20 +234,20 @@ gptr _myrealloc (register gptr pPtr, register uint uSize, if (check_ptr("Reallocating",(byte*) pPtr,sFile,uLine)) DBUG_RETURN((gptr) NULL); - pRec = (struct remember *) ((char*) pPtr - sizeof (struct irem)- + pRec = (struct remember *) ((char*) pPtr - ALIGN_SIZE(sizeof(struct irem))- sf_malloc_prehunc); if (*((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) != MAGICKEY) { - fprintf (stderr, "Reallocating unallocated data at line %d, '%s'\n", - uLine, sFile); + fprintf(stderr, "Error: Reallocating unallocated data at line %d, '%s'\n", + uLine, sFile); DBUG_PRINT("safe",("Reallocating unallocated data at line %d, '%s'", uLine, sFile)); (void) fflush(stderr); DBUG_RETURN((gptr) NULL); } - if ((ptr=_mymalloc(uSize,sFile,uLine,MyFlags))) /* Allocate new area */ + if ((ptr=_mymalloc(uSize,sFile,uLine,MyFlags))) /* Allocate new area */ { uSize=min(uSize,pRec-> uDataSize); /* Move as much as possibly */ memcpy((byte*) ptr,pPtr,(size_t) uSize); /* Copy old data */ @@ -257,10 +264,7 @@ gptr _myrealloc (register gptr pPtr, register uint uSize, } /* _myrealloc */ -/* - * void _myfree( my_string pPtr, my_string sFile, uint uLine, myf myflags) - * Deallocate some memory. - */ +/* Deallocate some memory. */ void _myfree (gptr pPtr, const char *sFile, uint uLine, myf myflags) { @@ -276,21 +280,23 @@ void _myfree (gptr pPtr, const char *sFile, uint uLine, myf myflags) DBUG_VOID_RETURN; /* Calculate the address of the remember structure */ - pRec = (struct remember *) ((byte*) pPtr-sizeof(struct irem)- + pRec = (struct remember *) ((byte*) pPtr- ALIGN_SIZE(sizeof(struct irem))- sf_malloc_prehunc); - /* Check to make sure that we have a real remember structure */ - /* Note: this test could fail for four reasons: */ - /* (1) The memory was already free'ed */ - /* (2) The memory was never new'ed */ - /* (3) There was an underrun */ - /* (4) A stray pointer hit this location */ + /* + Check to make sure that we have a real remember structure. + Note: this test could fail for four reasons: + (1) The memory was already free'ed + (2) The memory was never new'ed + (3) There was an underrun + (4) A stray pointer hit this location + */ if (*((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) != MAGICKEY) { - fprintf (stderr, "Freeing unallocated data at line %d, '%s'\n", - uLine, sFile); + fprintf(stderr, "Error: Freeing unallocated data at line %d, '%s'\n", + uLine, sFile); DBUG_PRINT("safe",("Unallocated data at line %d, '%s'",uLine,sFile)); (void) fflush(stderr); DBUG_VOID_RETURN; @@ -330,17 +336,17 @@ static int check_ptr(const char *where, byte *ptr, const char *sFile, { if (!ptr) { - fprintf (stderr, "%s NULL pointer at line %d, '%s'\n", - where,uLine, sFile); + fprintf(stderr, "Error: %s NULL pointer at line %d, '%s'\n", + where,uLine, sFile); DBUG_PRINT("safe",("Null pointer at line %d '%s'", uLine, sFile)); (void) fflush(stderr); return 1; } #ifndef _MSC_VER - if ((long) ptr & (MY_ALIGN(1,sizeof(char *))-1)) + if ((long) ptr & (ALIGN_SIZE(1)-1)) { - fprintf (stderr, "%s wrong aligned pointer at line %d, '%s'\n", - where,uLine, sFile); + fprintf(stderr, "Error: %s wrong aligned pointer at line %d, '%s'\n", + where,uLine, sFile); DBUG_PRINT("safe",("Wrong aligned pointer at line %d, '%s'", uLine,sFile)); (void) fflush(stderr); @@ -349,8 +355,8 @@ static int check_ptr(const char *where, byte *ptr, const char *sFile, #endif if (ptr < sf_min_adress || ptr > sf_max_adress) { - fprintf (stderr, "%s pointer out of range at line %d, '%s'\n", - where,uLine, sFile); + fprintf(stderr, "Error: %s pointer out of range at line %d, '%s'\n", + where,uLine, sFile); DBUG_PRINT("safe",("Pointer out of range at line %d '%s'", uLine,sFile)); (void) fflush(stderr); @@ -361,9 +367,9 @@ static int check_ptr(const char *where, byte *ptr, const char *sFile, /* - * TERMINATE(FILE *file) - * Report on all the memory pieces that have not been - * free'ed as well as the statistics. + TERMINATE(FILE *file) + Report on all the memory pieces that have not been + free'ed as well as the statistics. */ void TERMINATE (FILE *file) @@ -372,28 +378,32 @@ void TERMINATE (FILE *file) DBUG_ENTER("TERMINATE"); pthread_mutex_lock(&THR_LOCK_malloc); - /* Report the difference between number of calls to */ - /* NEW and the number of calls to FREE. >0 means more */ - /* NEWs than FREEs. <0, etc. */ + /* + Report the difference between number of calls to + NEW and the number of calls to FREE. >0 means more + NEWs than FREEs. <0, etc. + */ if (cNewCount) { if (file) { - fprintf (file, "cNewCount: %d\n", cNewCount); + fprintf(file, "Warning: Not freed memory segments: %d\n", cNewCount); (void) fflush(file); } DBUG_PRINT("safe",("cNewCount: %d",cNewCount)); } - /* Report on all the memory that was allocated with NEW */ - /* but not free'ed with FREE. */ + /* + Report on all the memory that was allocated with NEW + but not free'ed with FREE. + */ if ((pPtr=pRememberRoot)) { if (file) { - fprintf(file, "Memory that was not free'ed (%ld bytes):\n",lCurMemory); + fprintf(file, "Warning: Memory that was not free'ed (%ld bytes):\n",lCurMemory); (void) fflush(file); } DBUG_PRINT("safe",("Memory that was not free'ed (%ld bytes):",lCurMemory)); @@ -401,11 +411,12 @@ void TERMINATE (FILE *file) { if (file) { - fprintf (file, - "\t%6u bytes at 0x%09lx, allocated at line %4u in '%s'\n", - pPtr -> uDataSize, - (ulong) &(pPtr -> aData[sf_malloc_prehunc]), - pPtr -> uLineNum, pPtr -> sFileName); + fprintf(file, + "\t%6u bytes at 0x%09lx, allocated at line %4u in '%s'", + pPtr -> uDataSize, + (ulong) &(pPtr -> aData[sf_malloc_prehunc]), + pPtr -> uLineNum, pPtr -> sFileName); + fprintf(file, "\n"); (void) fflush(file); } DBUG_PRINT("safe", @@ -418,8 +429,8 @@ void TERMINATE (FILE *file) /* Report the memory usage statistics */ if (file) { - fprintf (file, "Maximum memory usage: %ld bytes (%ldk)\n", - lMaxMemory, (lMaxMemory + 1023L) / 1024L); + fprintf(file, "Maximum memory usage: %ld bytes (%ldk)\n", + lMaxMemory, (lMaxMemory + 1023L) / 1024L); (void) fflush(file); } DBUG_PRINT("safe",("Maximum memory usage: %ld bytes (%ldk)", @@ -442,9 +453,9 @@ static int _checkchunk (register struct remember *pRec, const char *sFile, if (*((long*) ((char*) &pRec -> lSpecialValue+sf_malloc_prehunc)) != MAGICKEY) { - fprintf (stderr, "Memory allocated at %s:%d was underrun,", - pRec -> sFileName, pRec -> uLineNum); - fprintf (stderr, " discovered at %s:%d\n", sFile, uLine); + fprintf(stderr, "Error: Memory allocated at %s:%d was underrun,", + pRec -> sFileName, pRec -> uLineNum); + fprintf(stderr, " discovered at %s:%d\n", sFile, uLine); (void) fflush(stderr); DBUG_PRINT("safe",("Underrun at %lx, allocated at %s:%d", &(pRec -> aData[sf_malloc_prehunc]), @@ -461,9 +472,9 @@ static int _checkchunk (register struct remember *pRec, const char *sFile, *magicp++ != MAGICEND2 || *magicp++ != MAGICEND3) { - fprintf (stderr, "Memory allocated at %s:%d was overrun,", - pRec -> sFileName, pRec -> uLineNum); - fprintf (stderr, " discovered at '%s:%d'\n", sFile, uLine); + fprintf(stderr, "Error: Memory allocated at %s:%d was overrun,", + pRec -> sFileName, pRec -> uLineNum); + fprintf(stderr, " discovered at '%s:%d'\n", sFile, uLine); (void) fflush(stderr); DBUG_PRINT("safe",("Overrun at %lx, allocated at %s:%d", &(pRec -> aData[sf_malloc_prehunc]), @@ -484,14 +495,19 @@ int _sanity (const char *sFile, uint uLine) uint count=0; pthread_mutex_lock(&THR_LOCK_malloc); +#ifndef PEDANTIC_SAFEMALLOC + if (sf_malloc_tampered && cNewCount < 0) + cNewCount=0; +#endif count=cNewCount; for (pTmp = pRememberRoot; pTmp != NULL && count-- ; pTmp = pTmp -> pNext) flag+=_checkchunk (pTmp, sFile, uLine); pthread_mutex_unlock(&THR_LOCK_malloc); if (count || pTmp) { - const char *format="Safemalloc link list destroyed, discovered at '%s:%d'"; - fprintf (stderr, format, sFile, uLine); fputc('\n',stderr); + const char *format="Error: Safemalloc link list destroyed, discovered at '%s:%d'"; + fprintf(stderr, format, sFile, uLine); fputc('\n',stderr); + fprintf(stderr, "root=%p,count=%d,pTmp=%p\n", pRememberRoot,count,pTmp); (void) fflush(stderr); DBUG_PRINT("safe",(format, sFile, uLine)); flag=1; @@ -512,12 +528,26 @@ gptr _my_memdup(const byte *from, uint length, const char *sFile, uint uLine, } /*_my_memdup */ -my_string _my_strdup(const char *from, const char *sFile, uint uLine, - myf MyFlags) +char *_my_strdup(const char *from, const char *sFile, uint uLine, + myf MyFlags) { gptr ptr; uint length=(uint) strlen(from)+1; if ((ptr=_mymalloc(length,sFile,uLine,MyFlags)) != 0) memcpy((byte*) ptr, (byte*) from,(size_t) length); - return((my_string) ptr); + return((char*) ptr); } /* _my_strdup */ + + +char *_my_strdup_with_length(const byte *from, uint length, + const char *sFile, uint uLine, + myf MyFlags) +{ + gptr ptr; + if ((ptr=_mymalloc(length+1,sFile,uLine,MyFlags)) != 0) + { + memcpy((byte*) ptr, (byte*) from,(size_t) length); + ptr[length]=0; + } + return((char *) ptr); +} diff --git a/mysys/sha1.c b/mysys/sha1.c new file mode 100644 index 00000000000..5271b369b6c --- /dev/null +++ b/mysys/sha1.c @@ -0,0 +1,392 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + Original Source from: http://www.faqs.org/rfcs/rfc3174.html + + DESCRIPTION + This file implements the Secure Hashing Algorithm 1 as + defined in FIPS PUB 180-1 published April 17, 1995. + + The SHA-1, produces a 160-bit message digest for a given data + stream. It should take about 2**n steps to find a message with the + same digest as a given message and 2**(n/2) to find any two + messages with the same digest, when n is the digest size in bits. + Therefore, this algorithm can serve as a means of providing a + "fingerprint" for a message. + + PORTABILITY ISSUES + SHA-1 is defined in terms of 32-bit "words". This code uses + <stdint.h> (included via "sha1.h" to define 32 and 8 bit unsigned + integer types. If your C compiler does not support 32 bit unsigned + integers, this code is not appropriate. + + CAVEATS + SHA-1 is designed to work with messages less than 2^64 bits long. + Although SHA-1 allows a message digest to be generated for messages + of any number of bits less than 2^64, this implementation only + works with messages with a length that is a multiple of the size of + an 8-bit character. + + CHANGES + 2002 by Peter Zaitsev to + - fit to new prototypes according to MySQL standard + - Some optimizations + - All checking is now done in debug only mode + - More comments +*/ + +#include "my_global.h" +#include "m_string.h" +#include "sha1.h" + +/* + Define the SHA1 circular left shift macro +*/ + +#define SHA1CircularShift(bits,word) \ + (((word) << (bits)) | ((word) >> (32-(bits)))) + +/* Local Function Prototyptes */ +static void SHA1PadMessage(SHA1_CONTEXT*); +static void SHA1ProcessMessageBlock(SHA1_CONTEXT*); + + +/* + Initialize SHA1Context + + SYNOPSIS + sha1_reset() + context [in/out] The context to reset. + + DESCRIPTION + This function will initialize the SHA1Context in preparation + for computing a new SHA1 message digest. + + RETURN + SHA_SUCCESS ok + != SHA_SUCCESS sha Error Code. +*/ + + +const uint32 sha_const_key[5]= +{ + 0x67452301, + 0xEFCDAB89, + 0x98BADCFE, + 0x10325476, + 0xC3D2E1F0 +}; + + +int sha1_reset(SHA1_CONTEXT *context) +{ +#ifndef DBUG_OFF + if (!context) + return SHA_NULL; +#endif + + context->Length = 0; + context->Message_Block_Index = 0; + + context->Intermediate_Hash[0] = sha_const_key[0]; + context->Intermediate_Hash[1] = sha_const_key[1]; + context->Intermediate_Hash[2] = sha_const_key[2]; + context->Intermediate_Hash[3] = sha_const_key[3]; + context->Intermediate_Hash[4] = sha_const_key[4]; + + context->Computed = 0; + context->Corrupted = 0; + + return SHA_SUCCESS; +} + + +/* + Return the 160-bit message digest into the array provided by the caller + + SYNOPSIS + sha1_result() + context [in/out] The context to use to calculate the SHA-1 hash. + Message_Digest: [out] Where the digest is returned. + + DESCRIPTION + NOTE: The first octet of hash is stored in the 0th element, + the last octet of hash in the 19th element. + + RETURN + SHA_SUCCESS ok + != SHA_SUCCESS sha Error Code. +*/ + +int sha1_result(SHA1_CONTEXT *context, + uint8 Message_Digest[SHA1_HASH_SIZE]) +{ + int i; + +#ifndef DBUG_OFF + if (!context || !Message_Digest) + return SHA_NULL; + + if (context->Corrupted) + return context->Corrupted; +#endif + + if (!context->Computed) + { + SHA1PadMessage(context); + /* message may be sensitive, clear it out */ + bzero((char*) context->Message_Block,64); + context->Length = 0; /* and clear length */ + context->Computed = 1; + } + + for (i = 0; i < SHA1_HASH_SIZE; i++) + Message_Digest[i] = (int8)((context->Intermediate_Hash[i>>2] >> 8 + * ( 3 - ( i & 0x03 ) ))); + return SHA_SUCCESS; +} + + +/* + Accepts an array of octets as the next portion of the message. + + SYNOPSIS + sha1_input() + context [in/out] The SHA context to update + message_array An array of characters representing the next portion + of the message. + length The length of the message in message_array + + RETURN + SHA_SUCCESS ok + != SHA_SUCCESS sha Error Code. +*/ + +int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array, + unsigned length) +{ + if (!length) + return SHA_SUCCESS; + +#ifndef DBUG_OFF + /* We assume client konows what it is doing in non-debug mode */ + if (!context || !message_array) + return SHA_NULL; + if (context->Computed) + return (context->Corrupted= SHA_STATE_ERROR); + if (context->Corrupted) + return context->Corrupted; +#endif + + while (length--) + { + context->Message_Block[context->Message_Block_Index++]= + (*message_array & 0xFF); + context->Length += 8; /* Length is in bits */ + +#ifndef DBUG_OFF + /* + Then we're not debugging we assume we never will get message longer + 2^64 bits. + */ + if (context->Length == 0) + return (context->Corrupted= 1); /* Message is too long */ +#endif + + if (context->Message_Block_Index == 64) + { + SHA1ProcessMessageBlock(context); + } + message_array++; + } + return SHA_SUCCESS; +} + + +/* + Process the next 512 bits of the message stored in the Message_Block array. + + SYNOPSIS + SHA1ProcessMessageBlock() + + DESCRIPTION + Many of the variable names in this code, especially the single + character names, were used because those were the names used in + the publication. +*/ + +/* Constants defined in SHA-1 */ +static const uint32 K[]= +{ + 0x5A827999, + 0x6ED9EBA1, + 0x8F1BBCDC, + 0xCA62C1D6 +}; + + +static void SHA1ProcessMessageBlock(SHA1_CONTEXT *context) +{ + int t; /* Loop counter */ + uint32 temp; /* Temporary word value */ + uint32 W[80]; /* Word sequence */ + uint32 A, B, C, D, E; /* Word buffers */ + int index; + + /* + Initialize the first 16 words in the array W + */ + + for (t = 0; t < 16; t++) + { + index=t*4; + W[t] = context->Message_Block[index] << 24; + W[t] |= context->Message_Block[index + 1] << 16; + W[t] |= context->Message_Block[index + 2] << 8; + W[t] |= context->Message_Block[index + 3]; + } + + + for (t = 16; t < 80; t++) + { + W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); + } + + A = context->Intermediate_Hash[0]; + B = context->Intermediate_Hash[1]; + C = context->Intermediate_Hash[2]; + D = context->Intermediate_Hash[3]; + E = context->Intermediate_Hash[4]; + + for (t = 0; t < 20; t++) + { + temp= SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0]; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for (t = 20; t < 40; t++) + { + temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for (t = 40; t < 60; t++) + { + temp= (SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + + K[2]); + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for (t = 60; t < 80; t++) + { + temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + context->Intermediate_Hash[0] += A; + context->Intermediate_Hash[1] += B; + context->Intermediate_Hash[2] += C; + context->Intermediate_Hash[3] += D; + context->Intermediate_Hash[4] += E; + + context->Message_Block_Index = 0; +} + + +/* + Pad message + + SYNOPSIS + SHA1PadMessage() + context: [in/out] The context to pad + + DESCRIPTION + According to the standard, the message must be padded to an even + 512 bits. The first padding bit must be a '1'. The last 64 bits + represent the length of the original message. All bits in between + should be 0. This function will pad the message according to + those rules by filling the Message_Block array accordingly. It + will also call the ProcessMessageBlock function provided + appropriately. When it returns, it can be assumed that the message + digest has been computed. + +*/ + +void SHA1PadMessage(SHA1_CONTEXT *context) +{ + /* + Check to see if the current message block is too small to hold + the initial padding bits and length. If so, we will pad the + block, process it, and then continue padding into a second + block. + */ + + int i=context->Message_Block_Index; + + if (i > 55) + { + context->Message_Block[i++] = 0x80; + bzero((char*) &context->Message_Block[i], + sizeof(context->Message_Block[0])*(64-i)); + context->Message_Block_Index=64; + + /* This function sets context->Message_Block_Index to zero */ + SHA1ProcessMessageBlock(context); + + bzero((char*) &context->Message_Block[0], + sizeof(context->Message_Block[0])*56); + context->Message_Block_Index=56; + } + else + { + context->Message_Block[i++] = 0x80; + bzero((char*) &context->Message_Block[i], + sizeof(context->Message_Block[0])*(56-i)); + context->Message_Block_Index=56; + } + + /* + Store the message length as the last 8 octets + */ + + context->Message_Block[56] = (int8) (context->Length >> 56); + context->Message_Block[57] = (int8) (context->Length >> 48); + context->Message_Block[58] = (int8) (context->Length >> 40); + context->Message_Block[59] = (int8) (context->Length >> 32); + context->Message_Block[60] = (int8) (context->Length >> 24); + context->Message_Block[61] = (int8) (context->Length >> 16); + context->Message_Block[62] = (int8) (context->Length >> 8); + context->Message_Block[63] = (int8) (context->Length); + + SHA1ProcessMessageBlock(context); +} diff --git a/mysys/string.c b/mysys/string.c index 96a503c1179..ea747bc9847 100644 --- a/mysys/string.c +++ b/mysys/string.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Code for handling strings with can grow dynamicly. @@ -51,7 +50,7 @@ my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str, my_bool dynstr_set(DYNAMIC_STRING *str, const char *init_str) { - uint length; + uint length=0; DBUG_ENTER("dynstr_set"); if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length) diff --git a/mysys/test_charset.c b/mysys/test_charset.c index 6a5183d2cd3..15e46f3ff82 100644 --- a/mysys/test_charset.c +++ b/mysys/test_charset.c @@ -1,21 +1,20 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ - -#include <global.h> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include <my_global.h> #include <m_ctype.h> #include <my_sys.h> #include <mysql_version.h> diff --git a/mysys/test_dir.c b/mysys/test_dir.c index fafdde137b6..f3d220e942f 100644 --- a/mysys/test_dir.c +++ b/mysys/test_dir.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* TODO: Test all functions */ diff --git a/mysys/test_fn.c b/mysys/test_fn.c index 224b899ebe1..5a0546392ab 100644 --- a/mysys/test_fn.c +++ b/mysys/test_fn.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mysys_priv.h" diff --git a/mysys/testhash.c b/mysys/testhash.c index a8fbf800595..a1d14dc225d 100644 --- a/mysys/testhash.c +++ b/mysys/testhash.c @@ -1,23 +1,22 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* Test av hash libarary: stor test */ +/* Test of hash library: big test */ -#include <global.h> +#include <my_global.h> #include <my_sys.h> #include <hash.h> #include <m_string.h> @@ -37,6 +36,7 @@ static uint16 key1[1000]; #else my_bool hash_check(HASH *hash); #endif + void free_record(void *record); static byte *hash2_key(const byte *rec,uint *length, @@ -46,7 +46,7 @@ static byte *hash2_key(const byte *rec,uint *length, return (byte*) rec; } - /* Huvudprogrammet */ +/* main program */ int main(int argc,char *argv[]) { @@ -76,7 +76,7 @@ static int do_test() printf("- Creating hash\n"); if (hash_init(&hash,recant/2,0,6,0,free_record,0)) goto err; - printf("- Writing records:s\n"); + printf("- Writing records:\n"); for (i=0 ; i < recant ; i++) { @@ -243,8 +243,8 @@ err: } /* main */ - /* l{ser optioner */ - /* OBS! intierar endast DEBUG - ingen debuggning h{r ! */ +/* read options */ +/* NOTE! DBUG not initialised - no debugging here! */ static int get_options(int argc, char **argv) { @@ -265,7 +265,7 @@ static int get_options(int argc, char **argv) case 'I': case '?': printf("%s Ver 1.0 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE); - puts("TCX Datakonsult AB, by Monty, for your professional use\n"); + printf("MySQL AB, by Monty\n\n"); printf("Usage: %s [-?ABIKLWv] [-m#] [-t#]\n",progname); exit(0); case '#': @@ -275,9 +275,10 @@ static int get_options(int argc, char **argv) } } return 0; -} /* get options */ +} /* get_options */ + - /* Ge ett randomv{rde inom ett intervall 0 <=x <= n */ +/* Get a random number in the interval 0 <= x <= n */ static int rnd(int max_value) { diff --git a/mysys/thr_alarm.c b/mysys/thr_alarm.c index f51f27b7f51..add5335a7af 100644 --- a/mysys/thr_alarm.c +++ b/mysys/thr_alarm.c @@ -1,21 +1,23 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <global.h> +/* To avoid problems with alarms in debug code, we disable DBUG here */ +#undef DBUG_OFF +#define DBUG_OFF +#include <my_global.h> #if defined(THREAD) && !defined(DONT_USE_THR_ALARM) #include <errno.h> @@ -37,11 +39,14 @@ static my_bool alarm_aborted=1; my_bool thr_alarm_inited=0; +static sig_handler process_alarm_part2(int sig); + #if !defined(__WIN__) && !defined(__EMX__) && !defined(OS2) static pthread_mutex_t LOCK_alarm; static sigset_t full_signal_set; static QUEUE alarm_queue; +static uint max_used_alarms=0; pthread_t alarm_thread; #ifdef USE_ALARM_THREAD @@ -114,10 +119,10 @@ void init_thr_alarm(uint max_alarms) } /* -** Request alarm after sec seconds. -** A pointer is returned with points to a non-zero int when the alarm has been -** given. This can't be called from the alarm-handling thread. -** Returns 0 if no more alarms are allowed (aborted by process) + Request alarm after sec seconds. + A pointer is returned with points to a non-zero int when the alarm has been + given. This can't be called from the alarm-handling thread. + Returns 0 if no more alarms are allowed (aborted by process) */ bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm_data) @@ -138,13 +143,17 @@ bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm_data) pthread_sigmask(SIG_SETMASK,&old_mask,NULL); DBUG_RETURN(1); } - if (alarm_queue.elements == alarm_queue.max_elements) + if (alarm_queue.elements >= max_used_alarms) { - DBUG_PRINT("info", ("alarm queue full")); - fprintf(stderr,"Warning: thr_alarm queue is full\n"); - pthread_mutex_unlock(&LOCK_alarm); - pthread_sigmask(SIG_SETMASK,&old_mask,NULL); - DBUG_RETURN(1); + if (alarm_queue.elements == alarm_queue.max_elements) + { + DBUG_PRINT("info", ("alarm queue full")); + fprintf(stderr,"Warning: thr_alarm queue is full\n"); + pthread_mutex_unlock(&LOCK_alarm); + pthread_sigmask(SIG_SETMASK,&old_mask,NULL); + DBUG_RETURN(1); + } + max_used_alarms=alarm_queue.elements+1; } reschedule= (!alarm_queue.elements || (int) (((ALARM*) queue_top(&alarm_queue))->expire_time - now) > @@ -184,7 +193,7 @@ bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm_data) /* -** Remove alarm from list of alarms + Remove alarm from list of alarms */ void thr_end_alarm(thr_alarm_t *alarmed) @@ -216,29 +225,35 @@ void thr_end_alarm(thr_alarm_t *alarmed) printf("Warning: Didn't find alarm %lx in queue of %d alarms\n", (long) *alarmed, alarm_queue.elements); #endif - DBUG_PRINT("warning",("Didn't find alarm %lx in queue\n",*alarmed)); + DBUG_PRINT("warning",("Didn't find alarm %lx in queue\n", + (long) *alarmed)); } if (alarm_aborted && !alarm_queue.elements) + { delete_queue(&alarm_queue); - pthread_mutex_unlock(&LOCK_alarm); + pthread_mutex_unlock(&LOCK_alarm); + pthread_mutex_destroy(&LOCK_alarm); + } + else + pthread_mutex_unlock(&LOCK_alarm); pthread_sigmask(SIG_SETMASK,&old_mask,NULL); DBUG_VOID_RETURN; } - /* - Come here when some alarm in queue is due. - Mark all alarms with are finnished in list. - Shedule alarms to be sent again after 1-10 sec (many alarms at once) - If alarm_aborted is set then all alarms are given and resent - every second. - */ +/* + Come here when some alarm in queue is due. + Mark all alarms with are finnished in list. + Shedule alarms to be sent again after 1-10 sec (many alarms at once) + If alarm_aborted is set then all alarms are given and resent + every second. +*/ sig_handler process_alarm(int sig __attribute__((unused))) { sigset_t old_mask; - ALARM *alarm_data; - DBUG_ENTER("process_alarm"); - DBUG_PRINT("info",("sig: %d active alarms: %d",sig,alarm_queue.elements)); +/* + This must be first as we can't call DBUG inside an alarm for a normal thread +*/ #if THR_SERVER_ALARM == THR_CLIENT_ALARM if (!pthread_equal(pthread_self(),alarm_thread)) @@ -249,17 +264,42 @@ sig_handler process_alarm(int sig __attribute__((unused))) #ifdef DONT_REMEMBER_SIGNAL sigset(THR_CLIENT_ALARM,process_alarm); /* int. thread system calls */ #endif - DBUG_VOID_RETURN; + return; } #endif -#if defined(MAIN) && !defined(__bsdi__) - printf("process_alarm\n"); fflush(stdout); -#endif + /* + We have to do do the handling of the alarm in a sub function, + because otherwise we would get problems with two threads calling + DBUG_... functions at the same time (as two threads may call + process_alarm() at the same time + */ + #ifndef USE_ALARM_THREAD pthread_sigmask(SIG_SETMASK,&full_signal_set,&old_mask); pthread_mutex_lock(&LOCK_alarm); #endif + process_alarm_part2(sig); +#ifndef USE_ALARM_THREAD +#if defined(DONT_REMEMBER_SIGNAL) && !defined(USE_ONE_SIGNAL_HAND) + sigset(THR_SERVER_ALARM,process_alarm); +#endif + pthread_mutex_unlock(&LOCK_alarm); + pthread_sigmask(SIG_SETMASK,&old_mask,NULL); +#endif + return; +} + + +static sig_handler process_alarm_part2(int sig __attribute__((unused))) +{ + ALARM *alarm_data; + DBUG_ENTER("process_alarm"); + DBUG_PRINT("info",("sig: %d active alarms: %d",sig,alarm_queue.elements)); + +#if defined(MAIN) && !defined(__bsdi__) + printf("process_alarm\n"); fflush(stdout); +#endif if (alarm_queue.elements) { if (alarm_aborted) @@ -320,49 +360,50 @@ sig_handler process_alarm(int sig __attribute__((unused))) #endif } } -#ifndef USE_ALARM_THREAD -#if defined(DONT_REMEMBER_SIGNAL) && !defined(USE_ONE_SIGNAL_HAND) - sigset(THR_SERVER_ALARM,process_alarm); -#endif - pthread_mutex_unlock(&LOCK_alarm); - pthread_sigmask(SIG_SETMASK,&old_mask,NULL); -#endif DBUG_VOID_RETURN; } /* -** Shedule all alarms now. -** When all alarms are given, Free alarm memory and don't allow more alarms. + Shedule all alarms now. + When all alarms are given, Free alarm memory and don't allow more alarms. */ void end_thr_alarm(void) { DBUG_ENTER("end_thr_alarm"); - pthread_mutex_lock(&LOCK_alarm); if (!alarm_aborted) - { + { + my_bool deleted=0; + pthread_mutex_lock(&LOCK_alarm); DBUG_PRINT("info",("Resheduling %d waiting alarms",alarm_queue.elements)); alarm_aborted=1; /* mark aborted */ if (!alarm_queue.elements) + { + deleted= 1; delete_queue(&alarm_queue); + } if (pthread_equal(pthread_self(),alarm_thread)) alarm(1); /* Shut down everything soon */ else reschedule_alarms(); + pthread_mutex_unlock(&LOCK_alarm); + if (deleted) + pthread_mutex_destroy(&LOCK_alarm); } - pthread_mutex_unlock(&LOCK_alarm); DBUG_VOID_RETURN; } /* -** Remove another thread from the alarm + Remove another thread from the alarm */ void thr_alarm_kill(pthread_t thread_id) { uint i; + if (alarm_aborted) + return; pthread_mutex_lock(&LOCK_alarm); for (i=0 ; i < alarm_queue.elements ; i++) { @@ -380,9 +421,25 @@ void thr_alarm_kill(pthread_t thread_id) } +void thr_alarm_info(ALARM_INFO *info) +{ + pthread_mutex_lock(&LOCK_alarm); + info->next_alarm_time= 0; + info->max_used_alarms= max_used_alarms; + if ((info->active_alarms= alarm_queue.elements)) + { + ulong now=(ulong) time((time_t*) 0); + long time_diff; + ALARM *alarm_data= (ALARM*) queue_top(&alarm_queue); + time_diff= (long) (alarm_data->expire_time - now); + info->next_alarm_time= (ulong) (time_diff < 0 ? 0 : time_diff); + } + pthread_mutex_unlock(&LOCK_alarm); +} + /* -** This is here for thread to get interruptet from read/write/fcntl -** ARGSUSED + This is here for thread to get interruptet from read/write/fcntl + ARGSUSED */ #if THR_CLIENT_ALARM != SIGALRM || defined(USE_ALARM_THREAD) @@ -457,7 +514,7 @@ static void *alarm_handler(void *arg __attribute__((unused))) #endif /* USE_ALARM_THREAD */ /***************************************************************************** -** thr_alarm for OS/2 + thr_alarm for OS/2 *****************************************************************************/ #elif defined(__EMX__) || defined(OS2) @@ -488,7 +545,7 @@ sig_handler process_alarm(int sig __attribute__((unused))) /* -** Remove another thread from the alarm + Remove another thread from the alarm */ void thr_alarm_kill(pthread_t thread_id) @@ -586,8 +643,14 @@ void init_thr_alarm(uint max_alarm) DBUG_VOID_RETURN; } +void thr_alarm_info(ALARM_INFO *info) +{ + bzero((char*) info, sizeof(*info)); +} + + /***************************************************************************** -** thr_alarm for win95 + thr_alarm for win95 *****************************************************************************/ #else /* __WIN__ */ @@ -605,6 +668,7 @@ sig_handler process_alarm(int sig __attribute__((unused))) bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm) { + (*alrm)= &alarm->alarmed; if (alarm_aborted) { alarm->alarmed.crono=0; @@ -613,7 +677,6 @@ bool thr_alarm(thr_alarm_t *alrm, uint sec, ALARM *alarm) if (!(alarm->alarmed.crono=SetTimer((HWND) NULL,0, sec*1000, (TIMERPROC) NULL))) return 1; - (*alrm)= &alarm->alarmed; return 0; } @@ -638,7 +701,7 @@ bool thr_got_alarm(thr_alarm_t *alrm_ptr) void thr_end_alarm(thr_alarm_t *alrm_ptr) { thr_alarm_t alrm= *alrm_ptr; - if (alrm->crono) + if (alrm && alrm->crono) { KillTimer(NULL, alrm->crono); alrm->crono = 0; @@ -659,13 +722,18 @@ void init_thr_alarm(uint max_alarm) DBUG_VOID_RETURN; } +void thr_alarm_info(ALARM_INFO *info) +{ + bzero((char*) info, sizeof(*info)); +} + #endif /* __WIN__ */ #endif /* THREAD */ /**************************************************************************** -** Handling of MAIN + Handling of test case (when compiled with -DMAIN) ***************************************************************************/ #ifdef MAIN @@ -675,11 +743,11 @@ static pthread_cond_t COND_thread_count; static pthread_mutex_t LOCK_thread_count; static uint thread_count; -#ifdef HPUX +#ifdef HPUX10 typedef int * fd_set_ptr; #else typedef fd_set * fd_set_ptr; -#endif /* HPUX */ +#endif /* HPUX10 */ static void *test_thread(void *arg) { @@ -865,6 +933,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) pthread_attr_t thr_attr; int i,*param,error; sigset_t set; + ALARM_INFO alarm_info; MY_INIT(argv[0]); if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '#') @@ -925,6 +994,10 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) pthread_attr_destroy(&thr_attr); pthread_mutex_lock(&LOCK_thread_count); + thr_alarm_info(&alarm_info); + printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n", + alarm_info.active_alarms, alarm_info.max_used_alarms, + alarm_info.next_alarm_time); while (thread_count) { VOID(pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)); @@ -935,6 +1008,10 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) } } pthread_mutex_unlock(&LOCK_thread_count); + thr_alarm_info(&alarm_info); + printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n", + alarm_info.active_alarms, alarm_info.max_used_alarms, + alarm_info.next_alarm_time); printf("Test succeeded\n"); return 0; } diff --git a/mysys/thr_lock.c b/mysys/thr_lock.c index aa50877072d..61616a4cf2b 100644 --- a/mysys/thr_lock.c +++ b/mysys/thr_lock.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Read and write locks for Posix threads. All tread must acquire @@ -946,6 +945,54 @@ void thr_abort_locks(THR_LOCK *lock) } +/* + Abort all locks for specific table/thread combination + + This is used to abort all locks for a specific thread +*/ + +void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread) +{ + THR_LOCK_DATA *data; + DBUG_ENTER("thr_abort_locks_for_thread"); + + pthread_mutex_lock(&lock->mutex); + for (data= lock->read_wait.data; data ; data= data->next) + { + if (pthread_equal(thread, data->thread)) + { + DBUG_PRINT("info",("Aborting read-wait lock")); + data->type= TL_UNLOCK; /* Mark killed */ + pthread_cond_signal(data->cond); + data->cond= 0; /* Removed from list */ + + if (((*data->prev)= data->next)) + data->next->prev= data->prev; + else + lock->read_wait.last= data->prev; + } + } + for (data= lock->write_wait.data; data ; data= data->next) + { + if (pthread_equal(thread, data->thread)) + { + DBUG_PRINT("info",("Aborting write-wait lock")); + data->type= TL_UNLOCK; + pthread_cond_signal(data->cond); + data->cond= 0; + + if (((*data->prev)= data->next)) + data->next->prev= data->prev; + else + lock->write_wait.last= data->prev; + } + } + pthread_mutex_unlock(&lock->mutex); + DBUG_VOID_RETURN; +} + + + /* Upgrade a WRITE_DELAY lock to a WRITE_LOCK */ my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data) diff --git a/mysys/thr_mutex.c b/mysys/thr_mutex.c index fa7ae4f1e82..2aabe2f500a 100644 --- a/mysys/thr_mutex.c +++ b/mysys/thr_mutex.c @@ -1,33 +1,34 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000-2003 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* This makes a wrapper for mutex handling to make it easier to debug mutex */ -#include <global.h> +#include <my_global.h> #if defined(HAVE_LINUXTHREADS) && !defined (__USE_UNIX98) #define __USE_UNIX98 /* To get rw locks under Linux */ #endif -#include <m_string.h> #if defined(THREAD) && defined(SAFE_MUTEX) #undef SAFE_MUTEX /* Avoid safe_mutex redefinitions */ -#include <my_pthread.h> +#include "mysys_priv.h" +#include "my_static.h" +#include <m_string.h> #ifndef DO_NOT_REMOVE_THREAD_WRAPPERS /* Remove wrappers */ +#undef pthread_mutex_t #undef pthread_mutex_init #undef pthread_mutex_lock #undef pthread_mutex_unlock @@ -39,23 +40,63 @@ #endif #endif /* DO_NOT_REMOVE_THREAD_WRAPPERS */ +static pthread_mutex_t THR_LOCK_mutex; +static ulong safe_mutex_count= 0; /* Number of mutexes created */ +#ifdef SAFE_MUTEX_DETECT_DESTROY +static struct st_safe_mutex_info_t *safe_mutex_root= NULL; +#endif + +void safe_mutex_global_init(void) +{ + pthread_mutex_init(&THR_LOCK_mutex,MY_MUTEX_INIT_FAST); +} + + int safe_mutex_init(safe_mutex_t *mp, - const pthread_mutexattr_t *attr __attribute__((unused))) + const pthread_mutexattr_t *attr __attribute__((unused)), + const char *file __attribute__((unused)), + uint line __attribute__((unused))) { bzero((char*) mp,sizeof(*mp)); pthread_mutex_init(&mp->global,MY_MUTEX_INIT_ERRCHK); pthread_mutex_init(&mp->mutex,attr); + +#ifdef SAFE_MUTEX_DETECT_DESTROY + /* + Monitor the freeing of mutexes. This code depends on single thread init + and destroy + */ + if ((mp->info= (safe_mutex_info_t *) malloc(sizeof(safe_mutex_info_t)))) + { + struct st_safe_mutex_info_t *info =mp->info; + + info->init_file= (char *) file; + info->init_line= line; + info->prev= NULL; + info->next= NULL; + + pthread_mutex_lock(&THR_LOCK_mutex); + if ((info->next= safe_mutex_root)) + safe_mutex_root->prev= info; + safe_mutex_root= info; + safe_mutex_count++; + pthread_mutex_unlock(&THR_LOCK_mutex); + } +#else + thread_safe_increment(safe_mutex_count, &THR_LOCK_mutex); +#endif /* SAFE_MUTEX_DETECT_DESTROY */ return 0; } + int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line) { int error; pthread_mutex_lock(&mp->global); if (mp->count > 0 && pthread_equal(pthread_self(),mp->thread)) { - fprintf(stderr,"safe_mutex: Trying to lock mutex at %s, line %d, when the mutex was already locked at %s, line %d\n", - file,line,mp->file,mp->line); + fprintf(stderr,"safe_mutex: Trying to lock mutex at %s, line %d, when the mutex was already locked at %s, line %d in thread %s\n", + file,line,mp->file, mp->line, my_thread_name()); fflush(stderr); abort(); } @@ -70,7 +111,8 @@ int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line) } if (mp->count++) { - fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, line %d more than 1 time\n", file,line); + fprintf(stderr,"safe_mutex: Error in thread libray: Got mutex at %s, \ +line %d more than 1 time\n", file,line); fflush(stderr); abort(); } @@ -108,7 +150,7 @@ int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line) error=pthread_mutex_unlock(&mp->mutex); if (error) { - fprintf(stderr,"safe_mutex: Got error: %d when trying to unlock mutex at %s, line %d\n", error, file, line); + fprintf(stderr,"safe_mutex: Got error: %d (%d) when trying to unlock mutex at %s, line %d\n", error, errno, file, line); fflush(stderr); abort(); } @@ -149,7 +191,7 @@ int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp, const char *file, pthread_mutex_lock(&mp->global); if (error) { - fprintf(stderr,"safe_mutex: Got error: %d when doing a safe_mutex_wait at %s, line %d\n", error, file, line); + fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_wait at %s, line %d\n", error, errno, file, line); fflush(stderr); abort(); } @@ -187,18 +229,23 @@ int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp, #ifdef EXTRA_DEBUG if (error && (error != EINTR && error != ETIMEDOUT)) { - fprintf(stderr,"safe_mutex: Got error: %d when doing a safe_mutex_timedwait at %s, line %d\n", error, file, line); + fprintf(stderr,"safe_mutex: Got error: %d (%d) when doing a safe_mutex_timedwait at %s, line %d\n", error, errno, file, line); } #endif pthread_mutex_lock(&mp->global); if (mp->count++) { fprintf(stderr, - "safe_mutex: Count was %d in thread %lx when locking mutex at %s, line %d (error: %d)\n", - mp->count-1, my_thread_id(), file, line, error); + "safe_mutex: Count was %d in thread %lx when locking mutex at %s, line %d (error: %d (%d))\n", + mp->count-1, my_thread_id(), file, line, error, error); fflush(stderr); abort(); } +#ifdef __NETWARE__ + /* NetWare doesn't re-acquire the mutex on an error */ + if (error && pthread_mutex_lock(&mp->mutex)) + mp->count--; +#endif /* __NETWARE__ */ mp->thread=pthread_self(); mp->file= (char*) file; mp->line=line; @@ -206,6 +253,7 @@ int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp, return error; } + int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line) { int error=0; @@ -225,7 +273,70 @@ int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line) if (pthread_mutex_destroy(&mp->mutex)) error=1; #endif + +#ifdef SAFE_MUTEX_DETECT_DESTROY + if (mp->info) + { + struct st_safe_mutex_info_t *info= mp->info; + pthread_mutex_lock(&THR_LOCK_mutex); + + if (info->prev) + info->prev->next = info->next; + else + safe_mutex_root = info->next; + if (info->next) + info->next->prev = info->prev; + safe_mutex_count--; + + pthread_mutex_unlock(&THR_LOCK_mutex); + free(info); + mp->info= NULL; /* Get crash if double free */ + } +#else + thread_safe_sub(safe_mutex_count, 1, &THR_LOCK_mutex); +#endif /* SAFE_MUTEX_DETECT_DESTROY */ return error; } + +/* + Free global resources and check that all mutex has been destroyed + + SYNOPSIS + safe_mutex_end() + file Print errors on this file + + NOTES + We can't use DBUG_PRINT() here as we have in my_end() disabled + DBUG handling before calling this function. + + In MySQL one may get one warning for a mutex created in my_thr_init.c + This is ok, as this thread may not yet have been exited. +*/ + +void safe_mutex_end(FILE *file __attribute__((unused))) +{ + if (!safe_mutex_count) /* safetly */ + pthread_mutex_destroy(&THR_LOCK_mutex); +#ifdef SAFE_MUTEX_DETECT_DESTROY + if (!file) + return; + + if (safe_mutex_count) + { + fprintf(file, "Warning: Not destroyed mutex: %lu\n", safe_mutex_count); + (void) fflush(file); + } + { + struct st_safe_mutex_info_t *ptr; + for (ptr= safe_mutex_root ; ptr ; ptr= ptr->next) + { + fprintf(file, "\tMutex initiated at line %4u in '%s'\n", + ptr->init_line, ptr->init_file); + (void) fflush(file); + } + } +#endif /* SAFE_MUTEX_DETECT_DESTROY */ +} + #endif /* THREAD && SAFE_MUTEX */ diff --git a/mysys/thr_rwlock.c b/mysys/thr_rwlock.c index d4fe79a06ac..29db2b997a0 100644 --- a/mysys/thr_rwlock.c +++ b/mysys/thr_rwlock.c @@ -1,29 +1,30 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Synchronization - readers / writer thread locks */ #include "mysys_priv.h" #if defined(THREAD) && !defined(HAVE_PTHREAD_RWLOCK_RDLOCK) && !defined(HAVE_RWLOCK_INIT) +#include <errno.h> /* - * Source base from Sun Microsystems SPILT, simplified - * for MySQL use -- Joshua Chamas - */ + Source base from Sun Microsystems SPILT, simplified for MySQL use + -- Joshua Chamas + Some cleanup and additional code by Monty +*/ /* * Multithreaded Demo Source @@ -58,7 +59,7 @@ * Mountain View, California 94043 */ -int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused))) +int my_rwlock_init(rw_lock_t *rwp, void *arg __attribute__((unused))) { pthread_condattr_t cond_attr; @@ -71,64 +72,101 @@ int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused))) rwp->state = 0; rwp->waiters = 0; - return( 0 ); + return(0); } -int my_rwlock_destroy( rw_lock_t *rwp ) { + +int my_rwlock_destroy(rw_lock_t *rwp) +{ pthread_mutex_destroy( &rwp->lock ); pthread_cond_destroy( &rwp->readers ); pthread_cond_destroy( &rwp->writers ); - - return( 0 ); + return(0); } -int my_rw_rdlock( rw_lock_t *rwp ) { + +int my_rw_rdlock(rw_lock_t *rwp) +{ pthread_mutex_lock(&rwp->lock); - /* active or queued writers */ - while ( ( rwp->state < 0 ) || rwp->waiters ) + /* active or queued writers */ + while (( rwp->state < 0 ) || rwp->waiters) pthread_cond_wait( &rwp->readers, &rwp->lock); rwp->state++; pthread_mutex_unlock(&rwp->lock); + return(0); +} - return( 0 ); +int my_rw_tryrdlock(rw_lock_t *rwp) +{ + int res; + pthread_mutex_lock(&rwp->lock); + if ((rwp->state < 0 ) || rwp->waiters) + res= EBUSY; /* Can't get lock */ + else + { + res=0; + rwp->state++; + } + pthread_mutex_unlock(&rwp->lock); + return(res); } -int my_rw_wrlock( rw_lock_t *rwp ) { +int my_rw_wrlock(rw_lock_t *rwp) +{ pthread_mutex_lock(&rwp->lock); - rwp->waiters++; /* another writer queued */ + rwp->waiters++; /* another writer queued */ - while ( rwp->state ) - pthread_cond_wait( &rwp->writers, &rwp->lock); + while (rwp->state) + pthread_cond_wait(&rwp->writers, &rwp->lock); rwp->state = -1; - --rwp->waiters; - pthread_mutex_unlock( &rwp->lock ); + rwp->waiters--; + pthread_mutex_unlock(&rwp->lock); + return(0); +} - return( 0 ); + +int my_rw_trywrlock(rw_lock_t *rwp) +{ + int res; + pthread_mutex_lock(&rwp->lock); + if (rwp->state) + res= EBUSY; /* Can't get lock */ + else + { + res=0; + rwp->state = -1; + } + pthread_mutex_unlock(&rwp->lock); + return(res); } -int my_rw_unlock( rw_lock_t *rwp ) { + +int my_rw_unlock(rw_lock_t *rwp) +{ DBUG_PRINT("rw_unlock", ("state: %d waiters: %d", rwp->state, rwp->waiters)); pthread_mutex_lock(&rwp->lock); - if ( rwp->state == -1 ) { /* writer releasing */ - rwp->state = 0; /* mark as available */ + if (rwp->state == -1) /* writer releasing */ + { + rwp->state= 0; /* mark as available */ - if ( rwp->waiters ) /* writers queued */ + if ( rwp->waiters ) /* writers queued */ pthread_cond_signal( &rwp->writers ); else pthread_cond_broadcast( &rwp->readers ); - } else { - if ( --rwp->state == 0 ) /* no more readers */ + } + else + { + if ( --rwp->state == 0 ) /* no more readers */ pthread_cond_signal( &rwp->writers ); } pthread_mutex_unlock( &rwp->lock ); - - return( 0 ); + return(0); } #endif diff --git a/mysys/tree.c b/mysys/tree.c index cd05a17fd72..ea5cf79f084 100644 --- a/mysys/tree.c +++ b/mysys/tree.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Code for handling red-black (balanced) binary trees. @@ -46,7 +45,8 @@ #define BLACK 1 #define RED 0 -#define DEFAULT_ALLOC_SIZE (8192-MALLOC_OVERHEAD) +#define DEFAULT_ALLOC_SIZE 8192 +#define DEFAULT_ALIGN_SIZE 8192 static void delete_tree_element(TREE *,TREE_ELEMENT *); static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *, @@ -62,28 +62,36 @@ static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent); /* The actuall code for handling binary trees */ -void init_tree(TREE *tree, uint default_alloc_size, int size, - qsort_cmp compare, my_bool with_delete, - void (*free_element) (void *)) +#ifndef DBUG_OFF +static int test_rb_tree(TREE_ELEMENT *element); +#endif + +void init_tree(TREE *tree, uint default_alloc_size, uint memory_limit, + int size, qsort_cmp2 compare, my_bool with_delete, + tree_element_free free_element, void *custom_arg) { DBUG_ENTER("init_tree"); DBUG_PRINT("enter",("tree: %lx size: %d",tree,size)); - if (!default_alloc_size) + if (default_alloc_size < DEFAULT_ALLOC_SIZE) default_alloc_size= DEFAULT_ALLOC_SIZE; + default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE); bzero((gptr) &tree->null_element,sizeof(tree->null_element)); tree->root= &tree->null_element; tree->compare=compare; tree->size_of_element=size > 0 ? (uint) size : 0; + tree->memory_limit=memory_limit; tree->free=free_element; + tree->allocated=0; tree->elements_in_tree=0; + tree->custom_arg = custom_arg; tree->null_element.colour=BLACK; tree->null_element.left=tree->null_element.right=0; if (!free_element && size >= 0 && ((uint) size <= sizeof(void*) || ((uint) size & (sizeof(void*)-1)))) { tree->offset_to_key=sizeof(TREE_ELEMENT); /* Put key after element */ - /* Fix allocation size so that we don't loose any memory */ + /* Fix allocation size so that we don't lose any memory */ default_alloc_size/=(sizeof(TREE_ELEMENT)+size); if (!default_alloc_size) default_alloc_size=1; @@ -102,9 +110,9 @@ void init_tree(TREE *tree, uint default_alloc_size, int size, DBUG_VOID_RETURN; } -void delete_tree(TREE *tree) +static void free_tree(TREE *tree, myf free_flags) { - DBUG_ENTER("delete_tree"); + DBUG_ENTER("free_tree"); DBUG_PRINT("enter",("tree: %lx",tree)); if (tree->root) /* If initialized */ @@ -114,24 +122,43 @@ void delete_tree(TREE *tree) else { if (tree->free) + { + if (tree->memory_limit) + (*tree->free)(NULL, free_init, tree->custom_arg); delete_tree_element(tree,tree->root); - free_root(&tree->mem_root,MYF(0)); + if (tree->memory_limit) + (*tree->free)(NULL, free_end, tree->custom_arg); + } + free_root(&tree->mem_root, free_flags); } } tree->root= &tree->null_element; tree->elements_in_tree=0; + tree->allocated=0; DBUG_VOID_RETURN; } +void delete_tree(TREE* tree) +{ + free_tree(tree, MYF(0)); /* my_free() mem_root if applicable */ +} + +void reset_tree(TREE* tree) +{ + free_tree(tree, MYF(MY_MARK_BLOCKS_FREE)); + /* do not my_free() mem_root if applicable, just mark blocks as free */ +} + + static void delete_tree_element(TREE *tree, TREE_ELEMENT *element) { if (element != &tree->null_element) { delete_tree_element(tree,element->left); - delete_tree_element(tree,element->right); if (tree->free) - (*tree->free)(ELEMENT_KEY(tree,element)); + (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg); + delete_tree_element(tree,element->right); if (tree->with_delete) my_free((char*) element,MYF(0)); } @@ -152,7 +179,8 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size) for (;;) { if (element == &tree->null_element || - (cmp=(*tree->compare)(ELEMENT_KEY(tree,element),key)) == 0) + (cmp=(*tree->compare)(tree->custom_arg, + ELEMENT_KEY(tree,element),key)) == 0) break; if (cmp < 0) { @@ -165,13 +193,22 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size) } if (element == &tree->null_element) { + uint alloc_size=sizeof(TREE_ELEMENT)+key_size+tree->size_of_element; + tree->allocated+=alloc_size; + + if (tree->memory_limit && tree->elements_in_tree + && tree->allocated > tree->memory_limit) + { + reset_tree(tree); + return tree_insert(tree, key, key_size); + } + key_size+=tree->size_of_element; if (tree->with_delete) - element=(TREE_ELEMENT *) my_malloc(sizeof(TREE_ELEMENT)+key_size, - MYF(MY_WME)); + element=(TREE_ELEMENT *) my_malloc(alloc_size, MYF(MY_WME)); else element=(TREE_ELEMENT *) - alloc_root(&tree->mem_root,sizeof(TREE_ELEMENT)+key_size); + alloc_root(&tree->mem_root,alloc_size); if (!element) return(NULL); **parent=element; @@ -195,6 +232,7 @@ TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size) } else element->count++; + DBUG_EXECUTE("check_tree", test_rb_tree(tree->root);); return element; } @@ -212,7 +250,8 @@ int tree_delete(TREE *tree, void *key) { if (element == &tree->null_element) return 1; /* Was not in tree */ - if ((cmp=(*tree->compare)(ELEMENT_KEY(tree,element),key)) == 0) + if ((cmp=(*tree->compare)(tree->custom_arg, + ELEMENT_KEY(tree,element),key)) == 0) break; if (cmp < 0) { @@ -252,7 +291,7 @@ int tree_delete(TREE *tree, void *key) if (remove_colour == BLACK) rb_delete_fixup(tree,parent); if (tree->free) - (*tree->free)(ELEMENT_KEY(tree,element)); + (*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg); my_free((gptr) element,MYF(0)); tree->elements_in_tree--; return 0; @@ -268,7 +307,8 @@ void *tree_search(TREE *tree, void *key) { if (element == &tree->null_element) return (void*) 0; - if ((cmp=(*tree->compare)(ELEMENT_KEY(tree,element),key)) == 0) + if ((cmp=(*tree->compare)(tree->custom_arg, + ELEMENT_KEY(tree,element),key)) == 0) return ELEMENT_KEY(tree,element); if (cmp < 0) element=element->right; @@ -484,8 +524,7 @@ static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent) x->colour=BLACK; } - -#ifdef TESTING_TREES +#ifndef DBUG_OFF /* Test that the proporties for a red-black tree holds */ @@ -511,5 +550,4 @@ static int test_rb_tree(TREE_ELEMENT *element) } return -1; } - #endif diff --git a/mysys/typelib.c b/mysys/typelib.c index b18959442ae..e2c8eade5c8 100644 --- a/mysys/typelib.c +++ b/mysys/typelib.c @@ -1,19 +1,18 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, +/* Copyright (C) 2000 MySQL AB + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library 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 */ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Functions to handle typelib */ @@ -84,7 +83,8 @@ int find_type(my_string x, TYPELIB *typelib, uint full_name) /* Get name of type nr 'nr' */ /* Warning first type is 1, 0 = empty field */ -void make_type(register my_string to, register uint nr, register TYPELIB *typelib) +void make_type(register my_string to, register uint nr, + register TYPELIB *typelib) { DBUG_ENTER("make_type"); if (!nr) |