Harald Welte | 4b233b4 | 2012-11-07 08:32:31 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2006 Raul Tremsal |
| 3 | * File : smpp34_dumpBuf.c |
| 4 | * Author: Raul Tremsal <ultraismo@yahoo.com> |
| 5 | * |
| 6 | * This file is part of libsmpp34 (c-open-smpp3.4 library). |
| 7 | * |
| 8 | * The libsmpp34 library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public License as |
| 10 | * published by the Free Software Foundation; either version 2.1 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 | * License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public License |
| 19 | * along with this library; if not, write to the Free Software Foundation, |
| 20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <malloc.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <netinet/in.h> |
| 28 | |
| 29 | #ifdef __linux__ |
| 30 | #include <stdint.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "smpp34.h" |
| 34 | #include "smpp34_structs.h" |
| 35 | #include "smpp34_params.h" |
| 36 | |
| 37 | |
| 38 | /* GLOBALS ********************************************************************/ |
| 39 | /* EXTERN *********************************************************************/ |
| 40 | extern int smpp34_errno; |
| 41 | extern char smpp34_strerror[2048]; |
| 42 | extern char *ptrerror; |
| 43 | |
| 44 | /* FUNCTIONS ******************************************************************/ |
| 45 | int |
| 46 | smpp34_dumpBuf(uint8_t *dest, int destL, uint8_t *src, int srcL) |
| 47 | { |
| 48 | |
| 49 | int i; |
| 50 | int j; |
| 51 | int size; |
| 52 | uint8_t ind = 3; |
| 53 | uint8_t *buffer = NULL; |
| 54 | int lefterror = 0; |
| 55 | |
| 56 | size = srcL; |
| 57 | buffer = src; |
| 58 | |
| 59 | memset(smpp34_strerror, 0, sizeof(smpp34_strerror)); |
| 60 | ptrerror = smpp34_strerror; |
| 61 | lefterror = sizeof(smpp34_strerror); |
| 62 | |
| 63 | /* dump buffer character by character until size is reached */ |
| 64 | for(i = 0; i < size; i++){ |
| 65 | switch( i % 16 ) { |
| 66 | case 0: |
| 67 | dest += sprintf((char*)dest, "%*c%02X ", ind, ' ', (uint8_t)buffer[i]); |
| 68 | break; |
| 69 | |
| 70 | case 7: |
| 71 | dest += sprintf((char*)dest, "%02X ", (uint8_t)buffer[i]); |
| 72 | break; |
| 73 | |
| 74 | case 15: |
| 75 | dest += sprintf((char*)dest, "%02X ", (uint8_t)buffer[i]); |
| 76 | for(j = (i - 15); j <= i; j++) { |
| 77 | if ( (buffer[j] < ' ') || (buffer[j] > '~') ) |
| 78 | dest += sprintf((char*)dest, "."); |
| 79 | else |
| 80 | dest += sprintf((char*)dest, "%c", buffer[j]); |
| 81 | if ( (j % 16) == 7 ) |
| 82 | dest += sprintf((char*)dest, " "); |
| 83 | } |
| 84 | dest += sprintf((char*)dest, "\n"); |
| 85 | break; |
| 86 | |
| 87 | default: |
| 88 | dest += sprintf((char*)dest, "%02X ", (uint8_t)buffer[i]); |
| 89 | break; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /* if the line is not completed, we have to fill it */ |
| 94 | if ( (size % 16) != 0 ) { |
| 95 | for (i = (size % 16); i < 16; i++) { |
| 96 | dest += sprintf((char*)dest, " "); |
| 97 | if ( (i % 16) == 7 ) |
| 98 | dest += sprintf((char*)dest, " "); |
| 99 | } |
| 100 | dest += sprintf((char*)dest, " "); |
| 101 | for (j = size - (size % 16); j < size; j++) { |
| 102 | /* check if character is printable */ |
| 103 | if ( (buffer[j] < ' ') || (buffer[j] > '~') ) |
| 104 | dest += sprintf((char*)dest, "."); |
| 105 | else |
| 106 | dest += sprintf((char*)dest, "%c", (char) buffer[j]); |
| 107 | if ( (j % 16) == 7 ) |
| 108 | dest += sprintf((char*)dest, " "); |
| 109 | } |
| 110 | dest += sprintf((char*)dest, "\n"); |
| 111 | } |
| 112 | |
| 113 | *dest = '\0'; |
| 114 | return( 0 ); |
| 115 | }; |
| 116 | |
| 117 | |