iskeyvalid.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: iskeyvalid.c /main/3 1995/10/23 11:42:06 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * iskeydesc.c
  33. *
  34. * Description:
  35. * Functions dealing with the keydesc structure.
  36. *
  37. *
  38. */
  39. #include "isam_impl.h"
  40. static int _check_typelen();
  41. /*
  42. * _validate_keydesc(keydesc, minreclen)
  43. *
  44. * Validate key descriptor. minreclen is needed to make suree that all
  45. * the key parts have a meaningful offset.
  46. *
  47. */
  48. int
  49. _validate_keydesc(struct keydesc *keydesc, int minreclen)
  50. {
  51. int nparts;
  52. int i;
  53. int type, start, length;
  54. int keylen = 0;
  55. nparts = keydesc->k_nparts;
  56. if (nparts <= 0 || nparts > NPARTS)
  57. return (ISERROR);
  58. for (i = 0; i < nparts;i++) {
  59. type = keydesc->k_part[i].kp_type & ~ISDESC;
  60. start = keydesc->k_part[i].kp_start;
  61. length = keydesc->k_part[i].kp_leng;
  62. if(_check_typelen(type, length) == ISERROR)
  63. return (ISERROR);
  64. if (type < MINTYPE || type >= MAXTYPE)
  65. return (ISERROR);
  66. if (start < 0 || start + length > minreclen)
  67. return (ISERROR);
  68. keylen += length;
  69. }
  70. if(keylen > MAXKEYSIZE)
  71. return (ISERROR);
  72. return (ISOK);
  73. }
  74. /*
  75. * _check_typelen()
  76. *
  77. * Check length against the length of the corresponding type.
  78. */
  79. static int
  80. _check_typelen(int type, int length)
  81. {
  82. switch (type) {
  83. case INTTYPE:
  84. return ((length == INTSIZE) ? ISOK : ISERROR);
  85. case LONGTYPE:
  86. return ((length == LONGSIZE) ? ISOK : ISERROR);
  87. case FLOATTYPE:
  88. return ((length == FLOATSIZE) ? ISOK : ISERROR);
  89. case DOUBLETYPE:
  90. return ((length == DOUBLESIZE) ? ISOK : ISERROR);
  91. case CHARTYPE:
  92. case BINTYPE:
  93. return ((length > 0) ? ISOK : ISERROR);
  94. default:
  95. return (ISERROR);
  96. }
  97. }