summaryrefslogblamecommitdiff
path: root/nuttx/arch/sim/src/up_deviceimage.c
blob: 7484240d075bb20548ba1ffa4d103dd994c94c79 (plain) (tree)









































































                                                                              







                                                                               
  

                                                                       



                                         





































                                                                           
















































































                                                                           
                                                                    

  
 










































































































































































                                                                                                     
/****************************************************************************
 * up_deviceimage.c
 *
 *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name Gregory Nutt nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#ifdef VFAT_STANDALONE
# include <stdio.h>
# include <stdlib.h>
# include <zlib.h>
#else
# include <nuttx/config.h>
# include <sys/types.h>
# include <stdlib.h>
# include <debug.h>
# include <zlib.h>
# include "up_internal.h"
#endif

/****************************************************************************
 * Private Definitions
 ****************************************************************************/

#ifdef VFAT_STANDALONE
# define dbg(format, arg...) printf(format, ##arg)
#endif

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* This array holds a compressed VFAT file system created with:
 *
 *   /sbin/mkdosfs -C -F 32 -I -n "NuttXTestVol" -S 512 -v nuttx-test.vfat 1024
 *   sudo mkdir /mnt/loop
 *   sudo mount -o loop nuttx-test.vfat /mnt/loop
 *   mkdir /mnt/loop/nuttx-test
 *   echo "This is a test" >/mnt/loop/nuttx-test/test-file.txt
 *   sudo umount /mnt/loop
 *   gzip nuttx-test.vfat
 *   xxd -g 1 nuttx-test.vfat.gz >some-file
 *
 * Then manually massaged from the gzip xxd output to zlib format.  See
 * http://www.faqs.org/rfcs/rfc1952.html
 */

static const unsigned char g_vfatdata[] =
{
    0x08, 0x1d, 0xed, 0xdd, 0x4d, 0x6b, 0x13, 0x41, 0x18, 0x00, 0xe0, 0x69,
    0x2d, 0xb6, 0x54, 0xfa, 0x71, 0x12, 0x3c, 0x39, 0x78, 0x53, 0x48, 0xa0,
    0x55, 0x7a, 0x36, 0x07, 0xab, 0xa7, 0x22, 0x75, 0x5b, 0x72, 0x12, 0x52,
    0x92, 0x6a, 0x68, 0x4c, 0x4a, 0x76, 0x8b, 0x2d, 0x48, 0x6f, 0xbd, 0xfb,
    0x2f, 0xc4, 0x62, 0x4f, 0x7a, 0x13, 0xc5, 0x3f, 0x90, 0x7f, 0xe1, 0x2d,
    0x97, 0x1e, 0x7b, 0x32, 0x6e, 0x4c, 0x53, 0x2c, 0x22, 0x7e, 0x80, 0x44,
    0xf1, 0x79, 0x76, 0x67, 0x66, 0xd9, 0x97, 0x81, 0x59, 0xe6, 0x65, 0x19,
    0xd8, 0x85, 0xe9, 0x96, 0x9f, 0x3f, 0xd9, 0xaa, 0xb6, 0xd2, 0xcd, 0x34,
    0x84, 0xf1, 0xb1, 0x18, 0xc6, 0x43, 0x08, 0x53, 0x27, 0x21, 0xc4, 0x70,
    0x3b, 0x0c, 0xcd, 0x9f, 0xb6, 0xfd, 0xd8, 0x58, 0xb8, 0x18, 0xce, 0xbb,
    0x7e, 0xfc, 0xfa, 0xee, 0xf2, 0xca, 0x4e, 0x96, 0x95, 0x93, 0x5a, 0x9a,
    0xad, 0xb7, 0x96, 0x4b, 0xc9, 0xcd, 0xc5, 0x18, 0xe3, 0xec, 0xd5, 0xf7,
    0x4f, 0x9f, 0xbd, 0xba, 0xf6, 0x21, 0xbb, 0xb4, 0xfe, 0x66, 0xf6, 0xed,
    0x64, 0xe8, 0xcc, 0x3f, 0xec, 0x1e, 0x2f, 0x7e, 0xec, 0x5c, 0xee, 0x5c,
    0xe9, 0x7e, 0x4a, 0x1e, 0xd7, 0xd3, 0x98, 0x9f, 0xcd, 0x56, 0x16, 0x2b,
    0x71, 0xa3, 0xd5, 0xca, 0x2a, 0x1b, 0x8d, 0x5a, 0xac, 0xd6, 0xd3, 0xad,
    0x62, 0x8c, 0xf7, 0x1b, 0xb5, 0x4a, 0x5a, 0x8b, 0xf5, 0x66, 0x5a, 0x6b,
    0x9f, 0x8b, 0x6f, 0x36, 0x5a, 0xdb, 0xdb, 0x7b, 0xb1, 0xd2, 0xac, 0xce,
    0x4c, 0x6f, 0xb7, 0x6b, 0x69, 0x9a, 0x5f, 0xee, 0xc5, 0xad, 0xda, 0x5e,
    0xcc, 0x5a, 0x31, 0x6b, 0xe7, 0x91, 0x47, 0x95, 0x7a, 0x33, 0x16, 0x8b,
    0xc5, 0x38, 0x33, 0x1d, 0xf8, 0x91, 0xb5, 0xc3, 0xd5, 0xd5, 0x4a, 0x69,
    0xd4, 0xa3, 0xe0, 0xcf, 0x6a, 0xb7, 0x4b, 0x95, 0x77, 0x93, 0x21, 0x4c,
    0x7c, 0x13, 0x59, 0x3b, 0x1c, 0xc1, 0x70, 0x00, 0x80, 0x11, 0xeb, 0x5a,
    0xff, 0xff, 0xc7, 0xac, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x76, 0x27, 0xbd, 0xde, 0x5c, 0x2f,
    0x2f, 0xc3, 0xb6, 0x5f, 0x46, 0x3d, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0xe0, 0xd7, 0xf8, 0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0xff, 0xbe, 0xaf, 0x36, 0xee, 0x9c, 0x0a, 0xe1, 0xc6, 0x8b,
    0xa3, 0xa5, 0xa3, 0xa5, 0x41, 0x3b, 0x88, 0x97, 0x9a, 0x61, 0x27, 0x64,
    0xf9, 0xb1, 0x1b, 0xe6, 0xc2, 0xbd, 0x42, 0x7e, 0x51, 0x0b, 0x69, 0x5e,
    0xf7, 0xf5, 0x72, 0x2b, 0x6b, 0x49, 0x52, 0x2e, 0xec, 0x2f, 0xc4, 0x18,
    0xe7, 0x43, 0xb5, 0xf3, 0x72, 0xd0, 0xbf, 0xdf, 0x5e, 0x18, 0xe1, 0x53,
    0xf1, 0xb3, 0x8a, 0xf1, 0xcc, 0x7c, 0x08, 0x07, 0xa7, 0xf3, 0x77, 0x70,
    0x36, 0x7f, 0xc5, 0xe2, 0xf7, 0xe2, 0x83, 0xfe, 0xa5, 0x61, 0x42, 0x14,
    0xf2, 0xfc, 0xb8, 0xb5, 0x19, 0xea, 0xa1, 0x91, 0xdf, 0x28, 0x7e, 0xc9,
    0x90, 0xdd, 0xbc, 0x4e, 0xee, 0x3c, 0x48, 0x0a, 0xcb, 0xfb, 0x0b, 0x49,
    0x39, 0x89, 0xe7, 0xf3, 0x63, 0x22, 0xf8, 0xd1, 0x64, 0xf4, 0x86, 0x1b,
    0xf1, 0x56, 0x62, 0x96, 0xbf, 0x04, 0xec, 0x99, 0x0b, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xbb, 0x3e, 0x03
};


/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: up_deviceimage
 *
 * Description: Return an allocated buffer representing an in-memory VFAT
 *   file system.
 *
 ****************************************************************************/

char *up_deviceimage(void)
{
    char    *pbuffer;
    int      bufsize = 1024*1024;
    int      offset  = 0;
    z_stream strm;
    int      ret;

    /* Ininitilize inflate state */

    strm.zalloc   = Z_NULL;
    strm.zfree    = Z_NULL;
    strm.opaque   = Z_NULL;
    strm.avail_in = 0;
    strm.next_in  = Z_NULL;
    ret           = inflateInit(&strm);
    if (ret != Z_OK)
      {
        dbg("inflateInit FAILED: ret=%d msg=\"%s\"\n", ret, strm.msg ? strm.msg : "No message" );
        return NULL;
      }

    /* Allocate a buffer to hold the decompressed buffer.  We may have
     * to reallocate this a few times to get the size right.
     */
    pbuffer = (char*)malloc(bufsize);

    /* Set up the input buffer */

    strm.avail_in = sizeof(g_vfatdata);
    strm.next_in = (Bytef*)g_vfatdata;

    /* Run inflate() on input until output buffer not full */

    do {
        /* Set up to catch the next output chunk in the output buffer */

        strm.avail_out = bufsize - offset;
        strm.next_out  = (Bytef*)&pbuffer[offset];

        /* inflate */

        ret = inflate(&strm, Z_NO_FLUSH);

        /* Handle inflate() error return values */

        switch (ret)
          {
            case Z_NEED_DICT:
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
            case Z_STREAM_ERROR:
                dbg("inflate FAILED: ret=%d msg=\"%s\"\n", ret, strm.msg ? strm.msg : "No message" );
                (void)inflateEnd(&strm);
                free(pbuffer);
                return NULL;
          }

        /* If avail_out is zero, then inflate() returned only
         * because it is out of buffer space.  In this case, we
         * will have to reallocate the buffer and try again.
         */

        if (strm.avail_out == 0)
          {
            int newbufsize = bufsize + 128*1024;
            char *newbuffer = realloc(pbuffer, newbufsize);
            if (!newbuffer)
              {
                free(pbuffer);
                return NULL;
              }
            else
              {
                pbuffer = newbuffer;
                offset  = bufsize;
                bufsize = newbufsize;
              }
          }
        else
          {
             /* There are unused bytes in the buffer, reallocate to
              * correct size.
              */

             int newbufsize = bufsize - strm.avail_out;
             char *newbuffer = realloc(pbuffer, newbufsize);
             if (!newbuffer)
               {
                free(pbuffer);
                return NULL;
              }
            else
              {
                pbuffer = newbuffer;
                bufsize = newbufsize;
              }
          }
    } while (strm.avail_out == 0 && ret != Z_STREAM_END);

    (void)inflateEnd(&strm);
    return pbuffer;
}

/****************************************************************************
 * Name: main
 *
 * Description: Used to test decompression logic
 *
 *  gcc -g -Wall -DVFAT_STANDALONE -lz -o vfat-test up_deviceimage.c
 *
 ****************************************************************************/

#ifdef VFAT_STANDALONE
int main(int argc, char **argv, char **envp)
{
    char *deviceimage;
    int cmf;
    int fdict;
    int flg;
    int check;

    cmf = g_vfatdata[0];
    printf("CMF=%02x: CM=%d CINFO=%d\n", cmf, cmf &0x0f, cmf >> 4);

    flg   = g_vfatdata[1];
    fdict = (flg >> 5) & 1;

    printf("FLG=%02x: FCHECK=%d FDICT=%d FLEVEL=%d\n", flg, flg &0x1f, fdict, flg >> 6);

    /* The FCHECK value must be such that CMF and FLG, when viewed as
     * a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
     * is a multiple of 31.
     */

    check = cmf*256 + flg;
    if (check % 31 != 0)
    {
        printf("Fails check: %04x is not a multiple of 31\n", check);
    }

    deviceimage = up_deviceimage();
    if (deviceimage)
      {
        printf("Inflate SUCCEEDED\n");
        free(deviceimage);
        return 0;
      }
    else
      {
        printf("Inflate FAILED\n");
        return 1;
      }
}
#endif