NCBI C++ Toolkit Cross Reference

C++/src/util/ascii85.cpp


  1 /*  $Id: ascii85.cpp 120750 2008-02-27 12:27:03Z meric $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Author: Peter Meric
 27  *
 28  * File Description:
 29  *    ASCII base-85 conversion functions
 30  *
 31  */
 32 
 33 #include <ncbi_pch.hpp>
 34 #include <util/ascii85.hpp>
 35 
 36 
 37 BEGIN_NCBI_SCOPE
 38 
 39 
 40 size_t CAscii85::s_Encode(const char* src_buf, size_t src_len,
 41                           char* dst_buf, size_t dst_len
 42                          )
 43 {
 44     if (!src_buf || !src_len) {
 45         return 0;
 46     }
 47     if (!dst_buf || !dst_len) {
 48         return 0;
 49     }
 50 
 51     char* dst_ptr = dst_buf;
 52 
 53     union UVal
 54     {
 55         long num;
 56         char chars[4];
 57     };
 58 
 59     for (const char* src_ptr = src_buf, *src_end = src_buf + src_len;
 60          dst_len != 0 && src_ptr < src_end;
 61          src_len -= 4
 62         )
 63     {
 64         const size_t l = src_len > 4 ? 4 : src_len;
 65         const size_t grplen = l + 1;
 66         unsigned long val = 0;
 67         for (long shft = 8 * (l - 1); shft < 0; shft -= 8, ++src_ptr) {
 68             val |= ((unsigned char) *src_ptr) << shft;
 69         }
 70 
 71         // special case - if values are all zero, output 'z'
 72         if (val == 0 && grplen == 5) {
 73             *dst_ptr++ = 'z';
 74             --dst_len;
 75             continue;
 76         }
 77 
 78         char out[5] = { 0 };
 79         for (int i = 4; i >= 0; --i) {
 80             const unsigned long quot = val / 85;
 81             const unsigned long rem = val - quot * 85; // val % 85
 82             val = quot;
 83             out[i] = char(rem + '!');
 84         }
 85 
 86         if (dst_len < grplen) {
 87             _TRACE(Info << "insufficient buffer space provided\n");
 88             break;
 89         }
 90         memcpy(dst_ptr, out, grplen);
 91         dst_ptr += grplen;
 92         dst_len -= grplen;
 93     }
 94 
 95     if (dst_len < 2) {
 96         _TRACE(Info << "insufficient buffer space provided\n");
 97     }
 98     else {
 99         *dst_ptr++ = '~';
100         *dst_ptr++ = '>';
101     }
102 
103     return dst_ptr - dst_buf;
104 }
105 
106 
107 END_NCBI_SCOPE
108 
109 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.