88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.56 2000/01/26 05:57:15 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.57 2000/03/24 02:41:46 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -41,13 +41,13 @@ static inttext_cmp(text *arg1, text *arg2);
4141 *The input is scaned twice.
4242 *The error checking of input is minimal.
4343 */
44- text *
44+ bytea *
4545byteain (char * inputText )
4646{
4747char * tp ;
4848char * rp ;
4949int byte ;
50- text * result ;
50+ bytea * result ;
5151
5252if (inputText == NULL )
5353elog (ERROR ,"Bad input string for type bytea" );
@@ -64,7 +64,7 @@ byteain(char *inputText)
6464}
6565tp = inputText ;
6666byte += VARHDRSZ ;
67- result = (text * )palloc (byte );
67+ result = (bytea * )palloc (byte );
6868result -> vl_len = byte ;/* varlena? */
6969rp = result -> vl_dat ;
7070while (* tp != '\0' )
@@ -90,10 +90,9 @@ byteain(char *inputText)
9090 *NULL vlena should be an error--returning string with NULL for now.
9191 */
9292char *
93- byteaout (text * vlena )
93+ byteaout (bytea * vlena )
9494{
9595char * result ;
96-
9796char * vp ;
9897char * rp ;
9998int val ;/* holds unprintable chars */
@@ -173,7 +172,6 @@ textin(char *inputText)
173172 *textout- converts internal representation to "..."
174173 */
175174char *
176-
177175textout (text * vlena )
178176{
179177int len ;
@@ -218,7 +216,7 @@ textlen(text *t)
218216#endif
219217
220218if (!PointerIsValid (t ))
221- elog ( ERROR , "Null input to textlen" ) ;
219+ return 0 ;
222220
223221#ifdef MULTIBYTE
224222len = 0 ;
@@ -247,10 +245,9 @@ int32
247245textoctetlen (text * t )
248246{
249247if (!PointerIsValid (t ))
250- elog ( ERROR , "Null input to textoctetlen" ) ;
248+ return 0 ;
251249
252250return VARSIZE (t )- VARHDRSZ ;
253-
254251}/* textoctetlen() */
255252
256253/*
@@ -621,19 +618,18 @@ text_smaller(text *arg1, text *arg2)
621618}
622619
623620/*-------------------------------------------------------------
624- *byteaGetSize
621+ *byteaoctetlen
625622 *
626623 * get the number of bytes contained in an instance of type 'bytea'
627624 *-------------------------------------------------------------
628625 */
629626int32
630- byteaGetSize ( text * v )
627+ byteaoctetlen ( bytea * v )
631628{
632- int len ;
633-
634- len = v -> vl_len - sizeof (v -> vl_len );
629+ if (!PointerIsValid (v ))
630+ return 0 ;
635631
636- return len ;
632+ return VARSIZE ( v ) - VARHDRSZ ;
637633}
638634
639635/*-------------------------------------------------------------
@@ -645,23 +641,22 @@ byteaGetSize(text *v)
645641 *-------------------------------------------------------------
646642 */
647643int32
648- byteaGetByte (text * v ,int32 n )
644+ byteaGetByte (bytea * v ,int32 n )
649645{
650646int len ;
651647int byte ;
652648
653- len = byteaGetSize (v );
649+ if (!PointerIsValid (v ))
650+ return 0 ;
654651
655- if (n >=len )
656- {
657- elog (ERROR ,"byteaGetByte: index (=%d) out of range [0..%d]" ,
652+ len = VARSIZE (v )- VARHDRSZ ;
653+
654+ if (n < 0 || n >=len )
655+ elog (ERROR ,"byteaGetByte: index %d out of range [0..%d]" ,
658656n ,len - 1 );
659- }
660- #ifdef USE_LOCALE
661- byte = (unsignedchar ) (v -> vl_dat [n ]);
662- #else
663- byte = v -> vl_dat [n ];
664- #endif
657+
658+ byte = ((unsignedchar * )VARDATA (v ))[n ];
659+
665660return (int32 )byte ;
666661}
667662
@@ -675,16 +670,26 @@ byteaGetByte(text *v, int32 n)
675670 *-------------------------------------------------------------
676671 */
677672int32
678- byteaGetBit (text * v ,int32 n )
673+ byteaGetBit (bytea * v ,int32 n )
679674{
680675int byteNo ,
681676bitNo ;
677+ int len ;
682678int byte ;
683679
680+ if (!PointerIsValid (v ))
681+ return 0 ;
682+
683+ len = VARSIZE (v )- VARHDRSZ ;
684+
685+ if (n < 0 || n >=len * 8 )
686+ elog (ERROR ,"byteaGetBit: index %d out of range [0..%d]" ,
687+ n ,len * 8 - 1 );
688+
684689byteNo = n /8 ;
685690bitNo = n %8 ;
686691
687- byte = byteaGetByte ( v , byteNo ) ;
692+ byte = (( unsigned char * ) VARDATA ( v ))[ byteNo ] ;
688693
689694if (byte & (1 <<bitNo ))
690695return (int32 )1 ;
@@ -700,36 +705,31 @@ byteaGetBit(text *v, int32 n)
700705 *
701706 *-------------------------------------------------------------
702707 */
703- text *
704- byteaSetByte (text * v ,int32 n ,int32 newByte )
708+ bytea *
709+ byteaSetByte (bytea * v ,int32 n ,int32 newByte )
705710{
706711int len ;
707- text * res ;
712+ bytea * res ;
708713
709- len = byteaGetSize (v );
714+ if (!PointerIsValid (v ))
715+ return 0 ;
710716
711- if ( n >= len )
712- {
713- elog ( ERROR ,
714- "byteaSetByte: index(=%d) out of range [0..%d]" ,
717+ len = VARSIZE ( v ) - VARHDRSZ ;
718+
719+ if ( n < 0 || n >= len )
720+ elog ( ERROR , "byteaSetByte: index%d out of range [0..%d]" ,
715721n ,len - 1 );
716- }
717722
718723/*
719724 * Make a copy of the original varlena.
720725 */
721- res = (text * )palloc (VARSIZE (v ));
722- if (res == NULL )
723- {
724- elog (ERROR ,"byteaSetByte: Out of memory (%d bytes requested)" ,
725- VARSIZE (v ));
726- }
727- memmove ((char * )res , (char * )v ,VARSIZE (v ));
726+ res = (bytea * )palloc (VARSIZE (v ));
727+ memcpy ((char * )res , (char * )v ,VARSIZE (v ));
728728
729729/*
730730 * Now set the byte.
731731 */
732- res -> vl_dat [n ]= newByte ;
732+ (( unsigned char * ) VARDATA ( res )) [n ]= newByte ;
733733
734734return res ;
735735}
@@ -742,26 +742,37 @@ byteaSetByte(text *v, int32 n, int32 newByte)
742742 *
743743 *-------------------------------------------------------------
744744 */
745- text *
746- byteaSetBit (text * v ,int32 n ,int32 newBit )
745+ bytea *
746+ byteaSetBit (bytea * v ,int32 n ,int32 newBit )
747747{
748- text * res ;
748+ bytea * res ;
749+ int len ;
749750int oldByte ,
750751newByte ;
751752int byteNo ,
752753bitNo ;
753754
755+ if (!PointerIsValid (v ))
756+ return NULL ;
757+
758+ len = VARSIZE (v )- VARHDRSZ ;
759+
760+ if (n < 0 || n >=len * 8 )
761+ elog (ERROR ,"byteaSetBit: index %d out of range [0..%d]" ,
762+ n ,len * 8 - 1 );
763+
764+ byteNo = n /8 ;
765+ bitNo = n %8 ;
766+
754767/*
755768 * sanity check!
756769 */
757770if (newBit != 0 && newBit != 1 )
758- elog (ERROR ,"byteaSetByte : new bit must be 0 or 1" );
771+ elog (ERROR ,"byteaSetBit : new bit must be 0 or 1" );
759772
760773/*
761774 * get the byte where the bit we want is stored.
762775 */
763- byteNo = n /8 ;
764- bitNo = n %8 ;
765776oldByte = byteaGetByte (v ,byteNo );
766777
767778/*