Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_buffer_utilities.c Source File

arm_uc_buffer_utilities.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-client-metadata-header/arm_uc_buffer_utilities.h"
00020 
00021 
00022 uint32_t arm_uc_crc32(const uint8_t *buffer, uint32_t length)
00023 {
00024     const uint8_t *current = buffer;
00025     uint32_t crc = 0xFFFFFFFF;
00026 
00027     while (length--) {
00028         crc ^= *current++;
00029 
00030         for (uint32_t counter = 0; counter < 8; counter++) {
00031             if (crc & 1) {
00032                 crc = (crc >> 1) ^ 0xEDB88320;
00033             } else {
00034                 crc = crc >> 1;
00035             }
00036         }
00037     }
00038 
00039     return (crc ^ 0xFFFFFFFF);
00040 }
00041 
00042 uint32_t arm_uc_parse_uint32(const uint8_t *input)
00043 {
00044     uint32_t result = 0;
00045 
00046     if (input) {
00047         result = input[0];
00048         result = (result << 8) | input[1];
00049         result = (result << 8) | input[2];
00050         result = (result << 8) | input[3];
00051     }
00052 
00053     return result;
00054 }
00055 
00056 uint64_t arm_uc_parse_uint64(const uint8_t *input)
00057 {
00058     uint64_t result = 0;
00059 
00060     if (input) {
00061         result = input[0];
00062         result = (result << 8) | input[1];
00063         result = (result << 8) | input[2];
00064         result = (result << 8) | input[3];
00065         result = (result << 8) | input[4];
00066         result = (result << 8) | input[5];
00067         result = (result << 8) | input[6];
00068         result = (result << 8) | input[7];
00069     }
00070 
00071     return result;
00072 }
00073 
00074 void arm_uc_write_uint32(uint8_t *buffer, uint32_t value)
00075 {
00076     if (buffer) {
00077         buffer[3] = value;
00078         buffer[2] = (value >> 8);
00079         buffer[1] = (value >> 16);
00080         buffer[0] = (value >> 24);
00081     }
00082 }
00083 
00084 void arm_uc_write_uint64(uint8_t *buffer, uint64_t value)
00085 {
00086     if (buffer) {
00087         buffer[7] = value;
00088         buffer[6] = (value >> 8);
00089         buffer[5] = (value >> 16);
00090         buffer[4] = (value >> 24);
00091         buffer[3] = (value >> 32);
00092         buffer[2] = (value >> 40);
00093         buffer[1] = (value >> 48);
00094         buffer[0] = (value >> 56);
00095     }
00096 }
00097 
00098 // Constant time binary comparison
00099 uint32_t ARM_UC_BinCompareCT(const arm_uc_buffer_t *a, const arm_uc_buffer_t *b)
00100 {
00101     uint32_t result;
00102     uint32_t i;
00103     const uint32_t bytes = a->size;
00104 
00105     // Check sizes
00106     if (a->size != b->size) {
00107         return 1;
00108     }
00109     result = 0;
00110     for (i = 0; i < bytes; i++) {
00111         result = result | (a->ptr[i] ^ b->ptr[i]);
00112     }
00113     // Reduce to 0 or 1 in constant time
00114     return (result | -result) >> 31;
00115 }