Main Page Modules Data Structures File List Data Fields Globals Related Pages

# beecrypt/blockpad.c

Go to the documentation of this file.

    00001
    00007 /*
    00008  * Copyright (c) 2000, 2001 Virtual Unlimited B.V.
    00009  *
    00010  * Author: Bob Deblier <bob@virtualunlimited.com>
    00011  *
    00012  * This library is free software; you can redistribute it and/or
    00013  * modify it under the terms of the GNU Lesser General Public
    00014  * License as published by the Free Software Foundation; either
    00015  * version 2.1 of the License, or (at your option) any later version.
    00016  *
    00017  * This library is distributed in the hope that it will be useful,
    00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00020  * Lesser General Public License for more details.
    00021  *
    00022  * You should have received a copy of the GNU Lesser General Public
    00023  * License along with this library; if not, write to the Free Software
    00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00025  *
    00026  */
    00027
    00028 #include "system.h"
    00029 #include "blockpad.h"
    00030 #include "debug.h"
    00031
    00032 /*@-boundswrite@*/
    00033 memchunk* pkcs5Pad(int blockbytes, memchunk* tmp)
    00034 {
    00035         if (tmp)
    00036         {
    00037                 byte padvalue = blockbytes - (tmp->size % blockbytes);
    00038
    00039                 tmp = memchunkResize(tmp, tmp->size + padvalue);
    00040
    00041                 if (tmp)
    00042                         memset(tmp->data - padvalue, padvalue, padvalue);
    00043         }
    00044
    00045         return tmp;
    00046 }
    00047 /*@=boundswrite@*/
    00048
    00049 /*@-boundsread@*/
    00050 memchunk* pkcs5Unpad(int blockbytes, memchunk* tmp)
    00051 {
    00052         if (tmp)
    00053         {
    00054                 byte padvalue;
    00055                 int i;
    00056
    00057                 if (tmp->data == (byte*) 0)
    00058                         return (memchunk*) 0;
    00059                 padvalue = tmp->data[tmp->size - 1];
    00060                 if (padvalue > blockbytes)
    00061                         return (memchunk*) 0;
    00062
    00063                 for (i = (tmp->size - padvalue); i < (tmp->size - 1); i++)
    00064                 {
    00065                         if (tmp->data[i] != padvalue)
    00066                                 return (memchunk*) 0;
    00067                 }
    00068
    00069                 tmp->size -= padvalue;
    00070 /*              tmp->data = (byte*) realloc(tmp->data, tmp->size; */
    00071         }
    00072
    00073         /*@-temptrans -compdef @*/
    00074         return tmp;
    00075         /*@=temptrans =compdef @*/
    00076 }
    00077 /*@=boundsread@*/
    00078
    00079 /*@-boundswrite@*/
    00080 memchunk* pkcs5PadCopy(int blockbytes, const memchunk* src)
    00081 {
    00082         memchunk* tmp;
    00083         byte padvalue = blockbytes - (src->size % blockbytes);
    00084
    00085         if (src == (memchunk*) 0)
    00086                 return (memchunk*) 0;
    00087
    00088         tmp = memchunkAlloc(src->size + padvalue);
    00089
    00090         if (tmp)
    00091         {
    00092                 memcpy(tmp->data, src->data, src->size);
    00093                 memset(tmp->data+src->size, padvalue, padvalue);
    00094         }
    00095
    00096         return tmp;
    00097 }
    00098 /*@=boundswrite@*/
    00099
    00100 /*@-boundswrite@*/
    00101 memchunk* pkcs5UnpadCopy(/*@unused@*/ int blockbytes, const memchunk* src)
    00102 {
    00103         memchunk* tmp;
    00104         byte padvalue;
    00105         int i;
    00106
    00107         if (src == (memchunk*) 0)
    00108                 return (memchunk*) 0;
    00109         if (src->data == (byte*) 0)
    00110                 return (memchunk*) 0;
    00111
    00112         padvalue = src->data[src->size - 1];
    00113
    00114         for (i = (src->size - padvalue); i < (src->size - 1); i++)
    00115         {
    00116                 if (src->data[i] != padvalue)
    00117                         return (memchunk*) 0;
    00118         }
    00119
    00120         tmp = memchunkAlloc(src->size - padvalue);
    00121
    00122         if (tmp)
    00123                 memcpy(tmp->data, src->data, tmp->size);
    00124
    00125         return tmp;
    00126 }
    00127 /*@=boundswrite@*/